<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.5.2version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.47version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>5.2.5.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>5.2.5.RELEASEversion>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatis-springartifactId>
<version>2.0.4version>
dependency>
<dependency>
<groupId>org.aspectjgroupId>
<artifactId>aspectjweaverartifactId>
<version>1.9.5version>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>1.18.12version>
dependency>
<build>
<resources>
<resource>
<directory>src/main/resourcesdirectory>
resource>
<resource>
<directory>src/main/javadirectory>
<includes>
<include>**/*.xmlinclude>
<include>**/*.propertiesinclude>
<include>**/*.tldinclude>
includes>
<filtering>falsefiltering>
resource>
resources>
build>
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true
jdbc.user=root
jdbc.password=root
注意:mybatis全局配置文件中的不需要配置数据源及后续用到的事务,都由spring全局配置文件中进行配置,进行统一管理和维护,让 mybatis.xml 配置文件配置简单化,一般只需要配置如下:
<configuration>
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
settings>
<typeAliases>
<package name="com.mage.vo"/>
typeAliases>
configuration>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="db.properties"/>
<bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
bean>
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="datasource"/>
<property name="configLocation" value="classpath:mybatis.xml"/>
<property name="mapperLocations" value="classpath:com/mage/mapper/*.xml"/>
bean>
<bean id="sessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg name="sqlSessionFactory" ref="sessionFactory"/>
bean>
<bean id="userDao" class="com.mage.dao.impl.UserDaoImpl">
<constructor-arg name="sqlSessionTemplate" ref="sessionTemplate"/>
bean>
beans>
注意点:
1.在基础的 MyBatis 用法中,是通过 SqlSessionFactoryBuilder 来创建 SqlSessionFactory 的。而在 MyBatis-Spring 中,则使用 SqlSessionFactoryBean 来创建
2.SqlSessionTemplate 是 MyBatis-Spring 的核心。作为 SqlSession 的一个实现,这意味着可以使用它无缝代替你代码中已经在使用的 SqlSession
3.SqlSessionTemplate 模板可以参与到 Spring 的事务管理中,并且由于其是线程安全的,可以供多个映射器类使用,你应该总是用 SqlSessionTemplate 来替换 MyBatis 默认的 DefaultSqlSession 实现。在同一应用程序中的不同类之间混杂使用可能会引起数据一致性的问题
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private Integer id;
private String userName;
private String userPwd;
private String flag;
private Date createtime;
}
public interface UserDao {
//根据用户的id查询,返回用户信息
public User queryUserById(Integer userId);
}
public class UserDaoImpl implements UserDao {
//注意,没有使用注解,我们需要在spring.xml配置文件中配置相应的bean对象,全部交给spring容器创建和维护
private SqlSessionTemplate sqlSessionTemplate;
public UserDaoImpl(SqlSessionTemplate sqlSessionTemplate) {
this.sqlSessionTemplate = sqlSessionTemplate;
}
public User queryUserById(Integer userId) {
UserDao userDao = sqlSessionTemplate.getMapper(UserDao.class);
return userDao.queryUserById(userId);
}
}
<mapper namespace="com.mage.dao.UserDao">
<select id="queryUserById" parameterType="int" resultType="User">
select id,user_name as userName,user_pwd as userPwd from user where id=#{id}
select>
mapper>
public class Test {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
UserDao userDaoImpl = applicationContext.getBean("userDao", UserDaoImpl.class);
User user = userDaoImpl.queryUserById(83);
System.out.println(user);
}
}
学到这里,整个整合过程就完成了,但我总感觉有点麻烦,每次都需要用到接口的实现类和依赖注入SqlSessionTemplate来开启会话,能不能和上一篇博客mybatis讲到的基于接口来进行CRUD呢,话不多说,直接开干,只需要增加或者改动少量配置,为了看的的清晰一点,我们重新整合一次
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-testartifactId>
<version>4.3.2.RELEASEversion>
<scope>testscope>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.5.2version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.47version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>5.2.5.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>5.2.5.RELEASEversion>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
<scope>testscope>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatis-springartifactId>
<version>2.0.4version>
dependency>
<dependency>
<groupId>org.aspectjgroupId>
<artifactId>aspectjweaverartifactId>
<version>1.9.5version>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>1.18.12version>
dependency>
dependencies>
<build>
<resources>
<resource>
<directory>src/main/resourcesdirectory>
resource>
<resource>
<directory>src/main/javadirectory>
<includes>
<include>**/*.xmlinclude>
<include>**/*.propertiesinclude>
<include>**/*.tldinclude>
includes>
<filtering>falsefiltering>
resource>
resources>
build>
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true
jdbc.user=root
jdbc.password=root
注意:mybatis全局配置文件中的不需要配置数据源及后续用到的事务,都由spring全局配置文件中进行配置,进行统一管理和维护,让 mybatis.xml 配置文件配置简单化,一般只需要配置如下:
<configuration>
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
settings>
<typeAliases>
<package name="com.mage.vo"/>
typeAliases>
configuration>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.mage"/>
<context:property-placeholder location="db.properties"/>
<bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
bean>
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="datasource"/>
<property name="configLocation" value="classpath:mybatis.xml"/>
<property name="mapperLocations" value="classpath:com/mage/mapper/*.xml"/>
bean>
<bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.mage.dao"/>
<property name="sqlSessionFactoryBeanName" value="sessionFactory"/>
bean>
beans>
注意点:
MapperScannerConfigurer支持由指定的创建接口或注解创建映射器
没有必要去指定 SqlSessionFactory 或 SqlSessionTemplate , 因为 MapperScannerConfigurer 将会创建MapperFactoryBean ,代替手工使用 SqlSessionDaoSupport 或 SqlSessionTemplate 编写数据访问对象(DAO)的代码,
MapperScannerConfigurer 将会查找类路径下的所有映射器接口并自动会创建一个MapperFactoryBean 实例,然后注入这个接口和 sqlSessionFactory ( mybatis 中提供的 SqlSessionFactory 接口,MapperFactoryBean 会使用SqlSessionFactory 创建 SqlSession ),之后想使用这个 Mapper 接口的话,直接通过 spring 注入这个bean,然后就可以直接使用了
如果你使用了一个以上的DataSource的情况下 ,你可以使用 sqlSessionFactoryBeanName。这就是它如何来配置的,注意 bean 的名称是必须的,而不是 bean 的引用,因 此,value 属性在这里替代通常的ref。
<property name="sqlSessionFactoryBeanName" value="sessionFactory"/>
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private Integer id;
private String userName;
private String userPwd;
private String flag;
private Date createtime;
}
public interface UserDao {
//根据用户的id查询,返回用户信息
public User queryUserById(Integer userId);
}
<mapper namespace="com.mage.dao.UserDao">
<select id="queryUserById" parameterType="int" resultType="User">
select id,user_name as userName,user_pwd as userPwd from user where id=#{id}
select>
mapper>
//集成测试在Spring容器环境下执行
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring.xml"})
public class Mytest {
@Autowired
private UserDao userDao;
@Test
public void test01(){
User user = userDao.queryUserById(83);
System.out.println(user);
}
}
本片博客分享就到这,如果有什么小问题,希望你在评论区留言,如果本片博客对你有帮助的话,希望你能收藏,想要学习更多,就多多关注我,文章持续更新中,别忘了点赞哦!谢谢!