引入依赖
<!-- 数据源 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.20</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.6</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.3</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.3</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<scope>compile</scope>
<version>5.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<scope>compile</scope>
<version>5.2.6.RELEASE</version>
</dependency>
Spring配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!-- 加载配置文件 -->
<context:property-placeholder location="classpath:mybatis/config.properties"/>
<!-- 数据库连接池 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url"
value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- spring和MyBatis完美整合 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- 自动扫描mapping.xml文件 -->
<property name="mapperLocations" value="classpath:mybatis/mapper/*.xml"></property>
<!--如果mybatis-config.xml没有特殊配置也可以不需要下面的配置-->
<property name="configLocation" value="classpath:mybatis/mybatis-config.xml" />
</bean>
<!-- DAO接口所在包名,Spring会自动查找其下的类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.example.start.springdemo.mybatis"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<!-- (事务管理)transaction manager -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>
删除mybatis-config中的Mapper
<?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>
<!-- 集成Spring,Properties可以不用配置 -->
<!-- 可以引用一个外部的properties文件 -->
<properties resource="mybatis/config.properties">
<!-- 也可以单独定义属性 -->
<!--<property name="username" value="root"/>-->
<!--<property name="password" value="Xiele"/>-->
</properties>
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<typeAliases>
<!-- 每个类都要配置一个别名 -->
<!--<typeAlias alias="User" type="com.example.start.springdemo.mybatis.User" />-->
<package name="com.example.start.springdemo.mybatis"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<!-- 集成Spring,这些代码需要删除掉 -->
<!--<environments default="development">-->
<!--<environment id="development">-->
<!--<transactionManager type="JDBC"/>-->
<!--<dataSource type="POOLED">-->
<!--<property name="driver" value="${jdbc.driver}"/>-->
<!--<property name="url" value="${jdbc.url}"/>-->
<!--<property name="username" value="${jdbc.username}"/>-->
<!--<property name="password" value="${jdbc.password}"/>-->
<!--</dataSource>-->
<!--</environment>-->
<!--</environments>-->
<!--<mappers>-->
<!--<mapper resource="mybatis/mapper/UserMapper.xml"/>-->
<!--</mappers>-->
</configuration>
Mybatis Mapper文件
路径: src/main/resources/mybatis/generator/sqlmap/SpringUserDao.xml
<?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.example.start.springdemo.mybatis.generator.SpringUserDao">
<resultMap id="BaseResultMap" type="com.example.start.springdemo.mybatis.generator.SpringUserDO">
<id column="id" property="id" />
<result column="name" property="name" />
<result column="age" property="age" />
</resultMap>
<sql id="Base_Column_List">
id, `name`, age
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from spring_user
where id = #{id}
</select>
<delete id="deleteByPrimaryKey">
delete from spring_user
where id = #{id}
</delete>
<insert id="insert">
insert into spring_user (id, `name`, age)
values (#{id}, #{name}, #{age})
</insert>
<insert id="insertSelective">
insert into spring_user
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
id,
</if>
<if test="name != null">
`name`,
</if>
<if test="age != null">
age,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id},
</if>
<if test="name != null">
#{name},
</if>
<if test="age != null">
#{age},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective">
update spring_user
<set>
<if test="name != null">
`name` = #{name},
</if>
<if test="age != null">
age = #{age},
</if>
</set>
where id = #{id}
</update>
<update id="updateByPrimaryKey">
update spring_user
set `name` = #{name},
age = #{age}
where id = #{id}
</update>
</mapper>
测试验证
手动获取Bean
@Test
public void testMybatisWithSpring() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("mybatis/mybatis-spring.xml");
SpringUserDao userDao = context.getBean(SpringUserDao.class);
SpringUserDO user = userDao.selectByPrimaryKey(1L);
System.out.println("mapper query -> " + user);
}
实际场景直接采用依赖注入
@Autowired
private SpringUserDao springUserDao;