Spring整合Mybatis 详解Spring怎样整合Mybatis
红旗下的小兵 人气:0想了解详解Spring怎样整合Mybatis的相关内容吗,红旗下的小兵在本文为您仔细讲解Spring整合Mybatis的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:spring整合mybatis步骤,spring整合mybatis,下面大家一起来学习吧。
第一步
导入相关jar包
<dependencies> <!--连接mysql--> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.21</version> </dependency> <!--加载mybatis--> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.6</version> </dependency> <!--junit测试--> <!-- https://mvnrepository.com/artifact/junit/junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.2</version> <scope>test</scope> </dependency> <!--spring-webmvc--> <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.8</version> </dependency> <!--spring-jdbc spring连接数据库必备包--> <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.3.8</version> </dependency> <!-- aspectjweaver - 在spring中使用动态代理--> <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver--> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.6</version> </dependency> <!--mybatis与spring整合的包--> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.6</version> </dependency> <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.20</version> <scope>provided</scope> </dependency> </dependencies> <!--下边配置:不管放在哪里的资源文件都会被编译--> <build> <resources> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>true</filtering> </resource> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>true</filtering> </resource> </resources> </build>
第二步
编写配置文件
resources下:
spring-config.xml,此配置文件下,连接数据库,创建SqlSessionFactory
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd"> <!--第一步:安装依赖包--> <!-- 第二步: 安装完依赖,使用Spring来管理数据源: DriverManagerDataSource 使用Spring的数据源替换Mybatis的配置,这里我们使用Spring提供的JDBC --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.cj.jdbc.Driver" /> <property name="url" value="jdbc:mysql://127.0.0.1:3306/java_pro?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&useSSL=false" /> <property name="username" value="root" /> <property name="password" value="lvxingchen" /> </bean> <!--创建sqlSessionFactory--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!--注入数据源,dataSource 就是上边我们配置的id="dataSource" --> <property name="dataSource" ref="dataSource" /> <!--绑定mybatis配置文件--> <property name="configLocation" value="classpath:mybatis-config.xml" /> <property name="mapperLocations" value="classpath:com/lxc/dao/UserMapper.xml" /> </bean> <!--创建sqlSession,SqlSessionTemplate 就是我们使用的sqlSession--> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <!--只能通过构造器注入sqlSessionFactory,因为没有set方法--> <constructor-arg index="0" ref="sqlSessionFactory" /> </bean> <!-- 把接口实现类 UserMapperImp 注入到spring中--> <bean id="userMapper" class="com.lxc.dao.UserMapperImp"> <property name="sqlSessionTemplate" ref="sqlSession"/> </bean> </beans>
mybatis-config.xml,下边 mybatis 的配置在上边 spring-config.xml 也可以,但是为了更加清晰,职责明确,把别名配置放在了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配置--> <configuration> <!--别名配置--> <typeAliases> <package name="com.lxc.domain" /> </typeAliases> </configuration>
实体类User
package com.lxc.domain; import lombok.Data; @Data public class User { private String name; private String password; }
UserMapper 接口
package com.lxc.dao; import com.lxc.domain.User; import java.util.List; public interface UserMapper { public List<User> getList(); }
UserMapperImp 实现接口
package com.lxc.dao; import com.lxc.domain.User; import org.mybatis.spring.SqlSessionTemplate; import java.util.List; // 《实现接口的类 UserMapperImp》 // 需要把这个类注入到Spring中去。 public class UserMapperImp implements UserMapper{ // 我们所有操作都是用sqlSession 来执行的 private SqlSessionTemplate sqlSession; public void setSqlSession(SqlSessionTemplate sqlSession) { this.sqlSession = sqlSession; } @Override public List<User> getList() { UserMapper userMapper = sqlSession.getMapper(UserMapper.class); return userMapper.getList(); } }
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.lxc.dao.UserMapper"> <select id="getList" resultType="User"> select * from mybatis </select> </mapper>
第三步
测试
import com.lxc.dao.UserMapper; import com.lxc.domain.User; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { @org.junit.Test public void test() { ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-config.xml"); UserMapper userMapper = ctx.getBean("userMapper", UserMapper.class); for (User item : userMapper.getList()) { System.out.println(item); } } }
输出:
加载全部内容