<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13</version> <scope>test</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.22</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.7</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.9</version> </dependency> <!--Spring操作数据库--> <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.3.9</version> </dependency> <!--AOP织入--> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.8.14</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.14</version> </dependency> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.6</version> </dependency> </dependencies>
package cn.iris.pojo; /** * @author Iris 2021/8/13 */ public class User { private int id; private String name; private String pwd; public User() { } public User(int id, String name, String pwd) { this.id = id; this.name = name; this.pwd = pwd; } 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; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } @Override public String toString() { return "User{" + "id=" + id + ", name='" + name + '\'' + ", pwd='" + pwd + '\'' + '}'; } }
<?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="cn.iris.mapper.UserMapper"> <select id="selectUser" resultType="User"> SELECT * FROM user; </select> </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> <properties resource="db.properties"/> <typeAliases> <package name="cn.iris.pojo"/> </typeAliases> <environments default="mybatis"> <environment id="mybatis"> <transactionManager type="JDBC"/><!-- 单独使用时配置成MANAGED没有事务 --> <dataSource type="POOLED"> <property name="driver" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </dataSource> </environment> </environments> <mappers> <mapper class="cn.iris.mapper.UserMapper"/> </mappers> </configuration>
在pom.xml文件中加入以下语句
<build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> <!-- 由于修改了默认的resource目录,导致src/main/resources的所有文件都不能被扫描,因此还要配多一个 --> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources> </build>
由Spring接管Myabtis业务
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean class="cn.iris.pojo.User"/> <!--Datasource 使用Spring的数据源替换Mybatis的配置--> <bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai"/> <property name="username" value="root"/> <property name="password" value="123456"/> </bean> <!--SqlSessionFactory--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="datasource"/> <!--绑定mybatis-config+mapper--> <property name="configLocation" value="classpath:mybatis-config.xml"/> <property name="mapperLocations" value="cn/iris/mapper/UserMapper.xml"/> </bean> <!--SqlSessionTemplate 等同于SqlSessionFactory--> <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate"> <!--只能使用构造器注入sqlSessionFactory(没有Setter)--> <constructor-arg index="0" ref="sqlSessionFactory"/> </bean> <!----> <bean id="userMapper" class="cn.iris.mapper.UserMapperImpl"> <property name="sqlSession" ref="sqlSessionTemplate"/> </bean> </beans>
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <import resource="spring-mapper.xml"/> </beans>
<?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> <typeAliases> <package name="cn.iris.pojo"/> </typeAliases> <!-- <settings>--> <!-- <setting name="" value=""/>--> <!-- </settings>--> </configuration>
package cn.iris.mapper; import cn.iris.pojo.User; import org.mybatis.spring.SqlSessionTemplate; import java.util.List; /** * @author Iris 2021/8/13 */ public class UserMapperImpl implements UserMapper{ // 之前:所有操作都用SQLSession执行 // 现在:都由SqlSessionTemplate执行 private SqlSessionTemplate sqlSession; public void setSqlSession(SqlSessionTemplate sqlSession) { this.sqlSession = sqlSession; } @Override public List<User> selectUser() { UserMapper mapper = sqlSession.getMapper(UserMapper.class); return mapper.selectUser(); } }
【测试】
@Test public void selectUser() throws IOException { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-mapper.xml"); UserMapper userMapper = context.getBean("userMapper", UserMapper.class); List<User> users = userMapper.selectUser(); for ( User user : users) { System.out.println(user); } } }
【SqlSessionDaoSupport(Spring5.2及以上版本支持)】
package cn.iris.mapper; import cn.iris.pojo.User; import org.mybatis.spring.support.SqlSessionDaoSupport; import java.util.List; /** * @author Iris 2021/8/13 */ public class UserMapperImpl2 extends SqlSessionDaoSupport implements UserMapper{ @Override public List<User> selectUser() { return getSqlSession().getMapper(UserMapper.class).selectUser(); } }
<bean id="userMapper2" class="cn.iris.mapper.UserMapperImpl2"> <property name="sqlSessionFactory" ref="sqlSessionFactory"/> </bean>
@Test public void selectUser() throws IOException { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationConfig.xml"); UserMapper userMapper = context.getBean("userMapper2", UserMapper.class); List<User> users = userMapper.selectUser(); for ( User user : users) { System.out.println(user); } }