Download area
File name File size
lib.rar 10m
spring_mybatis.rar 10m

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

Step 1 : be based on Mybatis My course   
Step 2 : pojo   
Step 3 : Mapper   
Step 4 : Category.xml   
Step 5 : applicationContext.xml   
Step 6 : Test   
Step 7 : Runnable project   

Step 1 :

be based on Mybatis My course

edit top fracture
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) { = id; } public String getName() { return name; } public void setName(String 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 "-// Mapper 3.0//EN" ""> <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="" xmlns:xsi="" xmlns:aop="" xmlns:tx="" xmlns:jdbc="" xmlns:context="" xmlns:mvc="" xsi:schemaLocation=""> <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; 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

Why Category.xml+CategoryMapper And ? Instead of using it alone Category.xml to configure sql Statement or alone in CategoryMapper Using annotations ? Is there any special purpose to divide it into two files ?( in addition Category There is no need to integrate into Spring Inside ?) solve

1 One answer

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
answer Or code please Fill in at least one , If you have a problem , Please ask again , Otherwise, the webmaster may not see

2021-02-20 Mapped Statements collection does not contain value for mapper.CategoryMapper.list

I use idea, What's wrong with this
java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for mapper.CategoryMapper.list

1 One answer

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
answer Or code please Fill in at least one , If you have a problem , Please ask again , Otherwise, the webmaster may not see

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
about JAVA Frame -Spring+Mybatis- Integration steps Your questions

Try to provide Screenshot code and Abnormal information , Help to analyze and solve problems . You can also enter this station QQ Group communication : 496725845
Ask questions and try to provide complete code , Environment description , The more conducive to the recurrence of the problem , The faster your question can be answered .
Have questions about the code in the tutorial , Please provide which step , Which line is in doubt , This makes it easy to quickly locate the problem , Improve the speed at which questions are answered
In the thousands of questions that already exist , A considerable proportion , Because of the use of and webmaster Different versions of the development environment Resulting in , For example jdk, eclpise, idea, mysql,tomcat Wait, the version of the software is inconsistent .
Please use the same version as the webmaster , You can save yourself a lot of learning time . The webmaster sorted out the software versions used in teaching , It's all here , Convenient for everyone to download : /k/helloworld/helloworld-version/1718.html

Upload screenshot