方式一:用SqlSessionTemplate
1.导包
- junit
- mysql
- mybatis
- spring
- aop织入
- mybatis-spring
- spring-jdbc
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.9</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.6</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.9</version>
</dependency>
</dependencies>
2.配置数据源
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/jdbcstudy?useUnicode=true&characterEncoding=utf8&useSSL=true&serverTimezone=UTC"></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
</bean>
3.配置SqlSessionFactory的bean
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:mybatis-conf.xml"></property>
</bean>
4.配置SqlSession的bean
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory"></constructor-arg>
</bean>
5.编写mapper接口
public interface UserMapper
{
public List<User> select();
}
6.编写mapper.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.xk.mapper.UserMapper">
<select id="select" resultType="com.xk.pojo.User">
select * from user
</select>
</mapper>
7.配置mapper的bean,注入SqlSession
<bean id="userMapper" class="com.xk.mapper.UserMapperImpl">
<property name="sqlSession" ref="sqlSession"></property>
</bean>
8.为了注入SqlSession,得实现mapper接口,写mapper接口的实现类
因为得写set方法,接口不能配bean
public class UserMapperImpl implements UserMapper
{
private SqlSessionTemplate sqlSession;
public void setSqlSession(SqlSessionTemplate sqlSession) {
this.sqlSession = sqlSession;
}
@Override
public List<User> select() {
return sqlSession.getMapper(UserMapper.class).select();
}
}
10.测试,从spring配置文件中获取mapper
public static void main(String[] args)
{
ApplicationContext context = new ClassPathXmlApplicationContext("applicationcontext.xml");
UserMapper userMapper = context.getBean("userMapper", UserMapper.class);
List<User> users=userMapper.select();
for (User user : users) {
System.out.println(user);
}
}
方式二:SqlSessionDaoSupport
|