十三. S+M
简单整合一下,Spring+SpringMVC+Mybatis还要在更仔细整合
1.maven下的mybatis.xml
<!-- junit进行单元测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!-- 导入mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!--导入 mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<!-- 导入 mybatis-spring-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.5</version>
</dependency>
<!-- aop织入 -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.5</version>
</dependency>
<!--静态资源过滤 -->
<build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>true</filtering> </resource> </resources> </build>
2.spring的xml配置
<!-- 导入spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
<!-- spring和数据库的整合-->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
3.数据库中存在一个user表,现在开始建立user类,测试一下能否正常和数据库相连接
userbean搞里头:
建立mybatis-config.xml文件,简单配置一下
<?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.why.bean"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql:
//localhost:3306/mybatis?useUnicode=true&characterEncoding=utf8"/>
<property name="username" value="root"/>
<property name="password" value="741886"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper class="com.why.mapper.UserMapper"/>
</mappers>
</configuration>
一个usermapper接口,搞里头
package com.why.mapper;
import com.why.bean.user;
import java.util.List;
/**
* @program: Spring+Mybatis
* @description:
* @author: @why
* @create: 2020-09-01 16:39
**/
public interface UserMapper {
public List<user> getAllMsg();
}
usermapper.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.why.mapper.UserMapper">
<select id="getAllMsg" resultType="user">
select * from user;
</select>
</mapper>
开始测试:
@Test
public void test() throws IOException {
String name="mybatis-config.xml";
InputStream stream = Resources.getResourceAsStream(name);
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(stream);
SqlSession sqlSession = factory.openSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
System.out.println(mapper.getAllMsg());
}
4.配置spring-dao.xml文件
mybatis-spring可以无缝将mybatis和spring整合一起。
我们之前都要自己写一个工具类,来获取sqlSession,但是,因为现在我们使用spring,因此我们所有的类都交给ioc容器管理,所以我们现在直接在spring的配置文件下配置SqlSessionFactory即可。
SqlSessionTemplate 是 MyBatis-Spring 的核心。 这个类负责管理 MyBatis 的 SqlSession, 调用 MyBatis 的 SQL 方法, 翻译异常。
<?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">
<!--配置数据源,可以使用spring默认的,也可以使用其他的数据源,如德鲁伊-->
<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource">
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf8"/>
<property name="password" value="root"/>
<property name="username" value="741886"/>
</bean>
<bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory">
<!-- 引用上面写的dataSource-->
<property name="dataSource" ref="dataSource"/>
<!-- 和mybatis关联-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="mapperLocations" value="classpath:com/why/mapper/*.xml"/>
</bean>
<!-- 配置:sessionTemplate-->
<bean class="org.mybatis.spring.SqlSessionTemplate" id="sessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
</beans>
增加一个对接口的实现类
package com.why.mapper;
import com.why.bean.User;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;
import java.util.List;
/**
* @program: Spring+Mybatis
* @description:
* @author: @why
* @create: 2020-09-01 17:39
**/
public class UserMapperIml implements UserMapper{
//现在 sqlSession不用我们管理了,交给spring管理了
private SqlSessionTemplate template;
public void setTemplate(SqlSessionTemplate template) {
this.template = template;
}
@Override
public List<User> getAllMsg() {
UserMapper mapper = template.getMapper(UserMapper.class);
return mapper.getAllMsg();
}
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationConfig.xml");
UserMapperIml userdao = context.getBean("userDao", UserMapperIml.class);
System.out.println(userdao.getAllMsg());
}
}
别忘了将这个实现注册到容器当中
<bean class="com.why.mapper.UserMapperIml" id="userDao">
<property name="template" ref="sessionTemplate"/>
</bean>
5.方式二:
继承类:SqlSessionDaoSupport
package com.why.mapper;
import com.why.bean.User;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;
import java.util.List;
/**
* @program: Spring+Mybatis
* @description:
* @author: @why
* @create: 2020-09-01 17:39
**/
public class UserMapperIml extends SqlSessionDaoSupport implements UserMapper{
@Override
public List<User> getAllMsg() {
UserMapper mapper = getSqlSession().getMapper(UserMapper.class);
return mapper.getAllMsg();
}
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationConfig.xml");
UserMapperIml userdao = context.getBean("userDao", UserMapperIml.class);
System.out.println(userdao.getAllMsg());
}
}
配置xml文件
<bean class="com.why.mapper.UserMapperIml" id="userDao">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>