Tool version compatibility problem
This tutorial is for follow-up study
SSM, Prepare to make the learning curve smoother . Used to demonstrate Spring and Mybatis How to integrate together
Correct
Step 2 : pojo Step 3 : Mapper Step 4 : Category.xml Step 5 : applicationContext.xml Step 6 : Test Step 7 : Runnable project
This tutorial is based on
Mybatis My course conduct , So before you start, I hope you build a database , Table structure ,Mybatis I'm familiar with the common usage of .
package com.how2java.pojo; public class Category { private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Category [id=" + id + ", name=" + name + "]"; } }
package com.how2java.mapper; import java.util.List; import com.how2java.pojo.Category; public interface CategoryMapper { public int add(Category category); public void delete(int id); public Category get(int id); public int update(Category category); public List<Category> list(); public int count(); }
Category.xml Need to be with the last CategoryMapper Class under the same package , And namespace Must write CategoryMapper
<mapper namespace="com.how2java.mapper.CategoryMapper">
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.how2java.mapper.CategoryMapper"> <insert id="add" parameterType="Category" > insert into category_ ( name ) values (#{name}) </insert> <delete id="delete" parameterType="Category" > delete from category_ where id= #{id} </delete> <select id="get" parameterType="_int" resultType="Category"> select * from category_ where id= #{id} </select> <update id="update" parameterType="Category" > update category_ set name=#{name} where id=#{id} </update> <select id="list" resultType="Category"> select * from category_ </select> </mapper>
<context:annotation-config /> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> Configure the driver to connect to the database ,URL, Account and password <bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean"> Scan XML Configuration file <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> Scan Mapper class
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <context:annotation-config /> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName"> <value>com.mysql.jdbc.Driver</value> </property> <property name="url"> <value>jdbc:mysql://localhost:3306/how2java?characterEncoding=UTF-8</value> </property> <property name="username"> <value>root</value> </property> <property name="password"> <value>admin</value> </property> </bean> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="typeAliasesPackage" value="com.how2java.pojo" /> <property name="dataSource" ref="dataSource"/> <property name="mapperLocations" value="classpath:com/how2java/mapper/*.xml"/> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.how2java.mapper"/> </bean> </beans>
use
Spring Annotation test , Get the injected CategoryMapper Object , When calling add When the method , Will automatically find Category.xml in id="add" of sql sentence .
package com.java.test; import java.util.List; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.how2java.mapper.CategoryMapper; import com.how2java.pojo.Category; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml") public class MybatisTest { @Autowired private CategoryMapper categoryMapper; @Test public void testAdd() { Category category = new Category(); category.setName("new Category"); categoryMapper.add(category); } @Test public void testList() { System.out.println(categoryMapper); List<Category> cs=categoryMapper.list(); for (Category c : cs) { System.out.println(c.getName()); } } }
In the upper right corner, there is the runnable Project Download corresponding to this knowledge point , I really can't do it myself , Just download and unzip it and compare it .
The official account of programming , Follow and get the latest tutorials and promotions in real time , thank you .
![]()
Q & A area
2021-06-24
about mybatis Integrate into Spring Some questions about
1 One answer
iweb_learn Jump to the problem location Answer time :2021-08-15
You need to look at the principle of proxy mode After reading it, you won't feel strange Whether it's xml Way or annotation It's just a way to configure You have to understand Mapper The function of the interface
The answer has been submitted successfully , Auditing . Please
My answer Check the answer record at , thank you
2021-02-20
Mapped Statements collection does not contain value for mapper.CategoryMapper.list
1 One answer
Autovy Jump to the problem location Answer time :2021-06-24
Category.xml Is it missing id="add" of sql sentence , This should be CategoryMapper of add() Method did not find the corresponding sql Statement results in
The answer has been submitted successfully , Auditing . Please
My answer Check the answer record at , thank you
2020-11-01
This content is a little brief
2020-10-29
That's it ?
2020-08-23
This tutorial has enough water
Too many questions , Page rendering is too slow , To speed up rendering , Only a few questions are displayed on this page at most . also 30 Previous questions , please Click to view
Please... Before asking questions land
The question has been submitted successfully , Auditing . Please
My question Check the question record at , thank you
|