<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency>
<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.2</version> </dependency>
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency>
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.1.10.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.1.10.RELEASE</version> </dependency>
<dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.4</version> </dependency>
<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.2</version> </dependency>
<build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>true</filtering> </resource> </resources> </build>
package com.xian.dao; public class User { private int id; private String name; @Override public String toString() { return "User{" + "id=" + id + ", name=‘" + name + ‘\‘‘ + ‘}‘; } }
package com.xian.Mapper; import com.xian.dao.User; import java.util.List; public interface UserMapper { //获取全部 public List<User> getUser(); }
package com.xian.Mapper; import com.xian.dao.User; import java.util.List; public interface UserMapper { //获取全部 public List<User> getUser(); }
<?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"> </properties> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${name}"/> <property name="password" value="${password}"/> </dataSource> </environment> </environments> <mappers>
//class定位类的属性
<mapper class="com.xian.dao.User"/>
</mappers>
</configuration>
db.properties
driver=com.mysql.cj.jdbc.Driver url=jdbc:mysql://localhost:3306/testdb?serverTimezone=UTC&useUnicode=ture&characterEncoding=UTF-8 name=root password=123456
测试类代替Utils类直接测试
public void selectUser() throws IOException { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession sqlSession = sqlSessionFactory.openSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); List<User> userList = mapper.getUser(); for (User user: userList){ System.out.println(user); } sqlSession.close(); }
User类,UserMapper接口和UserMapper不变
<?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="com.xian.dao.User"/> </typeAliases> </configuration>
<?xml version="1.0" encoding="GBK"?> <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 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/testdb?useSSL=true&useUnicode=true&characterEncoding=utf8"/> <property name="username" value="root"/> <property name="password" value="123456"/> </bean> <!-- 创建SQLsessionFaction,引用上面的数据源--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:mybatis-config.xml"/> <property name="mapperLocations" value="classpath:com/xian/Mapper/*.xml"/> </bean> <!-- SqlSessionTemplate 需要一个sqlSessionFactory 通过构造器注入--> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg index="0" ref="sqlSessionFactory" /> </bean> <!-- 为数据源添加一个接口实现类,并注册bean、set注入一个sqlsesion--> <bean id="userDao" class="com.xian.dao.UserImpl"> <property name="sqlSessionTemplate" ref="sqlSession" /> </bean> </beans>
package com.xian.dao; import com.xian.Mapper.UserMapper; import org.mybatis.spring.SqlSessionTemplate; import java.util.List; public class UserImpl implements UserMapper { private SqlSessionTemplate sqlSessionTemplate; public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) { this.sqlSessionTemplate = sqlSessionTemplate; } public List<User> getUser() { UserMapper mapper = sqlSessionTemplate.getMapper(UserMapper.class); return mapper.getUser(); } }
public void Test02(){ ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); UserImpl userDao = (UserImpl) context.getBean("userDao"); List<User> user = userDao.getUser(); System.out.println(user); }
public class UserImpl extends SqlSessionDaoSupport implements UserMapper { public List<User> getUser() { UserMapper mapper = getSqlSession().getMapper(UserMapper.class); return mapper.getUser(); } }
public void Test02(){ ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); UserImpl userDao = (UserImpl) context.getBean("userDao"); List<User> user = userDao.getUser(); System.out.println(user); }
原文:https://www.cnblogs.com/springxian/p/13725172.html