一·导入需要的jar包
<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>5.1.48</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.7</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.11</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.10</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.6</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
</dependency>
静态资源过滤问题解决:
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.tld</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.tld</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
二·方式一
在spring-dao.xml中注入MyBatis所需的一些对象,用spring里的一些类来创建:
创建数据源:
<!--创建数据源-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=false&useUnicode=true&characterEncoding=UTF8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
创建sqlSessionFactory:注入dataSource属性
<!--创建工厂-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!--连接配置文件-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<!--注册Mapper-->
<property name="mapperLocations" value="classpath:com/he/Mapper/*.xml"/>
</bean>
创建sqlSession:只能通过构造器来注入属性,内部没有set方法。sqlSessionTemplate与sqlSession相同。
<!-- 创建sqlSession-->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<!-- 只能通过构造器注入,内部没有set方法-->
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
创建一个实体类,实现UserMapper接口,一个属性为sqlSession, 并创建set方法方便注入属性,也可以直接使用@Autowired,将实体类注入spring容器,加上@Component即可。
@Component
public class UserMapperImpl implements UserMapper{
//sqlSessionTemplate就相当于sqlSession
@Autowired
private SqlSessionTemplate sqlSession;
public void setSqlSession(SqlSessionTemplate sqlSession) {
this.sqlSession = sqlSession;
}
@Override
public List<User> getUsers() {
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
return mapper.getUsers();
}
}
在applicationContext.xml中开启扫描bean:
<context:component-scan base-package="com.he"/>
applicationContext中引入:
<import resource="spring-dao.xml"/>
测试:
@Test
public void test() throws IOException {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserMapper userMapper = context.getBean("userMapperImpl", UserMapper.class);
List<User> users = userMapper.getUsers();
for (User user : users) {
System.out.println(user);
}
}
三·方式二
前面创建数据库的一些对象不变,创建一个新的实体类UserMapperImpl2,继承SqlSessionDaoSupport ,并实现UserMapper 接口,注入到spring容器中,通过getSqlSessionTemplate()方法或getSqlSession()方法返回一个sqlSession对象,即可进行操作。重写setSqlSessionFactory方法,将容器中的sqlSessionFactory注入到父类的属性中,即可通过sqlSessionFactory创建sqlSession
@Component
public class UserMapperImpl2 extends SqlSessionDaoSupport implements UserMapper {
@Override
@Autowired
public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
super.setSqlSessionFactory(sqlSessionFactory);
}
@Override
public List<User> getUsers() {
SqlSessionTemplate sqlSession = getSqlSessionTemplate();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
return mapper.getUsers();
}
}
省略了在spring容器中注册sqlSession 的步骤!更简便。
测试:
@Test
public void test2() throws IOException {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserMapper userMapper = context.getBean("userMapperImpl2", UserMapper.class);
List<User> users = userMapper.getUsers();
for (User user : users) {
System.out.println(user);
}
}