引入依赖

  1. <!-- 数据源 -->
  2. <dependency>
  3. <groupId>mysql</groupId>
  4. <artifactId>mysql-connector-java</artifactId>
  5. <version>8.0.20</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>com.alibaba</groupId>
  9. <artifactId>druid</artifactId>
  10. <version>1.1.6</version>
  11. </dependency>
  12. <dependency>
  13. <groupId>org.mybatis</groupId>
  14. <artifactId>mybatis</artifactId>
  15. <version>3.5.3</version>
  16. </dependency>
  17. <dependency>
  18. <groupId>org.mybatis</groupId>
  19. <artifactId>mybatis-spring</artifactId>
  20. <version>2.0.3</version>
  21. </dependency>
  22. <dependency>
  23. <groupId>org.springframework</groupId>
  24. <artifactId>spring-core</artifactId>
  25. <scope>compile</scope>
  26. <version>5.2.6.RELEASE</version>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.springframework</groupId>
  30. <artifactId>spring-context</artifactId>
  31. <scope>compile</scope>
  32. <version>5.2.6.RELEASE</version>
  33. </dependency>

Spring配置文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns:context="http://www.springframework.org/schema/context"
  4. xmlns="http://www.springframework.org/schema/beans"
  5. 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">
  6. <!-- 加载配置文件 -->
  7. <context:property-placeholder location="classpath:mybatis/config.properties"/>
  8. <!-- 数据库连接池 -->
  9. <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
  10. destroy-method="close">
  11. <property name="driverClassName" value="${jdbc.driver}"/>
  12. <property name="url"
  13. value="${jdbc.url}"/>
  14. <property name="username" value="${jdbc.username}"/>
  15. <property name="password" value="${jdbc.password}"/>
  16. </bean>
  17. <!-- spring和MyBatis完美整合 -->
  18. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  19. <property name="dataSource" ref="dataSource"/>
  20. <!-- 自动扫描mapping.xml文件 -->
  21. <property name="mapperLocations" value="classpath:mybatis/mapper/*.xml"></property>
  22. <!--如果mybatis-config.xml没有特殊配置也可以不需要下面的配置-->
  23. <property name="configLocation" value="classpath:mybatis/mybatis-config.xml" />
  24. </bean>
  25. <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
  26. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  27. <property name="basePackage" value="com.example.start.springdemo.mybatis"/>
  28. <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
  29. </bean>
  30. <!-- (事务管理)transaction manager -->
  31. <bean id="transactionManager"
  32. class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  33. <property name="dataSource" ref="dataSource"/>
  34. </bean>
  35. </beans>

删除mybatis-config中的Mapper

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE configuration
  3. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-config.dtd">
  5. <configuration>
  6. <!-- 集成Spring,Properties可以不用配置 -->
  7. <!-- 可以引用一个外部的properties文件 -->
  8. <properties resource="mybatis/config.properties">
  9. <!-- 也可以单独定义属性 -->
  10. <!--<property name="username" value="root"/>-->
  11. <!--<property name="password" value="Xiele"/>-->
  12. </properties>
  13. <settings>
  14. <setting name="logImpl" value="STDOUT_LOGGING"/>
  15. </settings>
  16. <typeAliases>
  17. <!-- 每个类都要配置一个别名 -->
  18. <!--<typeAlias alias="User" type="com.example.start.springdemo.mybatis.User" />-->
  19. <package name="com.example.start.springdemo.mybatis"/>
  20. </typeAliases>
  21. <environments default="development">
  22. <environment id="development">
  23. <transactionManager type="JDBC"/>
  24. <dataSource type="POOLED">
  25. <property name="driver" value="${jdbc.driver}"/>
  26. <property name="url" value="${jdbc.url}"/>
  27. <property name="username" value="${jdbc.username}"/>
  28. <property name="password" value="${jdbc.password}"/>
  29. </dataSource>
  30. </environment>
  31. </environments>
  32. <!-- 集成Spring,这些代码需要删除掉 -->
  33. <!--<environments default="development">-->
  34. <!--<environment id="development">-->
  35. <!--<transactionManager type="JDBC"/>-->
  36. <!--<dataSource type="POOLED">-->
  37. <!--<property name="driver" value="${jdbc.driver}"/>-->
  38. <!--<property name="url" value="${jdbc.url}"/>-->
  39. <!--<property name="username" value="${jdbc.username}"/>-->
  40. <!--<property name="password" value="${jdbc.password}"/>-->
  41. <!--</dataSource>-->
  42. <!--</environment>-->
  43. <!--</environments>-->
  44. <!--<mappers>-->
  45. <!--<mapper resource="mybatis/mapper/UserMapper.xml"/>-->
  46. <!--</mappers>-->
  47. </configuration>

Mybatis Mapper文件

路径: src/main/resources/mybatis/generator/sqlmap/SpringUserDao.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3. <mapper namespace="com.example.start.springdemo.mybatis.generator.SpringUserDao">
  4. <resultMap id="BaseResultMap" type="com.example.start.springdemo.mybatis.generator.SpringUserDO">
  5. <id column="id" property="id" />
  6. <result column="name" property="name" />
  7. <result column="age" property="age" />
  8. </resultMap>
  9. <sql id="Base_Column_List">
  10. id, `name`, age
  11. </sql>
  12. <select id="selectByPrimaryKey" resultMap="BaseResultMap">
  13. select
  14. <include refid="Base_Column_List" />
  15. from spring_user
  16. where id = #{id}
  17. </select>
  18. <delete id="deleteByPrimaryKey">
  19. delete from spring_user
  20. where id = #{id}
  21. </delete>
  22. <insert id="insert">
  23. insert into spring_user (id, `name`, age)
  24. values (#{id}, #{name}, #{age})
  25. </insert>
  26. <insert id="insertSelective">
  27. insert into spring_user
  28. <trim prefix="(" suffix=")" suffixOverrides=",">
  29. <if test="id != null">
  30. id,
  31. </if>
  32. <if test="name != null">
  33. `name`,
  34. </if>
  35. <if test="age != null">
  36. age,
  37. </if>
  38. </trim>
  39. <trim prefix="values (" suffix=")" suffixOverrides=",">
  40. <if test="id != null">
  41. #{id},
  42. </if>
  43. <if test="name != null">
  44. #{name},
  45. </if>
  46. <if test="age != null">
  47. #{age},
  48. </if>
  49. </trim>
  50. </insert>
  51. <update id="updateByPrimaryKeySelective">
  52. update spring_user
  53. <set>
  54. <if test="name != null">
  55. `name` = #{name},
  56. </if>
  57. <if test="age != null">
  58. age = #{age},
  59. </if>
  60. </set>
  61. where id = #{id}
  62. </update>
  63. <update id="updateByPrimaryKey">
  64. update spring_user
  65. set `name` = #{name},
  66. age = #{age}
  67. where id = #{id}
  68. </update>
  69. </mapper>

测试验证

手动获取Bean

  1. @Test
  2. public void testMybatisWithSpring() {
  3. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("mybatis/mybatis-spring.xml");
  4. SpringUserDao userDao = context.getBean(SpringUserDao.class);
  5. SpringUserDO user = userDao.selectByPrimaryKey(1L);
  6. System.out.println("mapper query -> " + user);
  7. }

实际场景直接采用依赖注入

  1. @Autowired
  2. private SpringUserDao springUserDao;