1、引入mybatis、spring-webmvc、spring-jdbc、mybatis-spring以及相关数据库依赖:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.21.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency>
2、编写spring配置文件:
<?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:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">
<!-- 读取db.properties文件 -->
<util:properties id="dbprop" location="classpath:db.properties"/>
<!-- 配置连接池 -->
<bean id="ds" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="#{dbprop['driver']}"/>
<property name="url" value="#{dbprop['url']}"/>
<property name="username" value="#{dbprop['user']}"/>
<property name="password" value="#{dbprop['pwd']}"/>
<property name="initialSize" value="#{dbprop.initSize}"/>
<property name="maxActive" value="#{dbprop.maxSize}"/>
</bean>
<!--
配置SqlSessionFactoryBean
该bean的作用是用来代替MyBatis配置文件
MyBatis配置文件:是用来指定连接池和mapper.xml文件路径的
-->
<bean id="ssb" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 指定连接资源 -->
<property name="dataSource" ref="ds"/>
<!-- 指定映射文件 -->
<property name="mapperLocations" value="classpath:org/spring/teach/entity/*.xml"/>
<!-- 指定mybatis配置文件,做而外配置 -->
<property name="configLocation" value="classpath:mybatisConfigure.xml"/>
</bean>
<!--
配置MapperScannerConfigurer
该bean会扫描指定包及其子包下面所有的Mapper映射器(接口),也就是dao,然后生成符合该接口要求的对象(通过调用SqlSession的getMapper方法),
接下来会将这些对象(即Mapper映射器的实现对象)添加到Spring容器里面(默认的id是首字母小写之后的接口名)
-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 指定要扫描的包 -->
<property name="basePackage" value="org.spring.teach.dao"/>
<!-- 还可以指定只扫描带有该注解的接口(可选) -->
<property name="annotationClass" value="org.spring.teach.annotations.MyBatisRepository"/>
</bean>
</beans>
3、编写mybatis配置文件:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<typeAliases>
<typeAlias type="org.spring.teach.entity.User" alias="User"/>
</typeAliases>
</configuration>
4、编写实体类、mapper映射接口、mapper映射文件:
package org.spring.teach.entity;
public class User {
private Integer id;
private String name;
private String username;
private String password;
private String gender;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", username='" + username + '\'' +
", password='" + password + '\'' +
", gender='" + gender + '\'' +
'}';
}
}
package org.spring.teach.dao;
import org.spring.teach.annotations.MyBatisRepository;
import org.spring.teach.entity.User;
import java.util.List;
@MyBatisRepository
public interface UserMapper {
List<User> findAll();
User findById(Integer id);
int save(User user);
int delete(Integer id);
int update(User user);
}
<?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="org.spring.teach.dao.UserMapper">
<insert id="save" parameterType="User">
insert into t_user values(t_user_seq.nextval,#{username},#{name},#{password},#{gender})
</insert>
<select id="findAll" resultType="User">
select * from t_user
</select>
<select id="findById" parameterType="int" resultType="User">
select * from t_user where id = #{id}
</select>
<update id="update" parameterType="User">
update t_user set username = #{username},name = #{name},password = #{password},gender = #{gender} where id = #{id}
</update>
<delete id="delete" parameterType="int">
delete from t_user where id = #{id1}
</delete>
</mapper>
5、测试:
public class TestCase {
private AbstractApplicationContext app;
private UserMapper dao;
@Before
public void before() {
app = new ClassPathXmlApplicationContext("spring-mybatis.xml");
dao = app.getBean("userMapper", UserMapper.class);
}
@After
public void after() {
app.close();
}
@Test
public void save() {
User user = new User();
user.setUsername("赵六");
user.setName("赵六");
user.setPassword("534634634");
user.setGender("男");
int row = dao.save(user);
System.out.println(row);
}
@Test
public void findAll() {
List<User> list = dao.findAll();
System.out.println(list);
}
@Test
public void findById() {
User user = dao.findById(1);
System.out.println(user);
}
@Test
public void update() {
User user = new User();
user.setId(62);
user.setUsername("赵六");
user.setName("赵六");
user.setPassword("gfnvcvsdv");
user.setGender("女");
int row = dao.update(user);
System.out.println(row);
}
@Test
public void delete() {
int row = dao.delete(64);
System.out.println(row);
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/99b9adba2399f20db53881217eedc9d4.png)