写点什么

【Spring 持久层】Spring 与 Mybatis 整合,spring 教程极客学院

  • 2021 年 11 月 10 日
  • 本文字数:2761 字

    阅读完需:约 9 分钟

);


  1. 创建 DAO 接口:UserDAO


public interface UserDAO {


public void save(User user);


}


  1. 实现 Mapper 文件:UserDAOMapper.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.yusael.mybatis.UserDAO">


<insert id="save" parameterType="user">


insert into t_users(name, password) values (#{name}, #{password})


</insert>


</mapper>


  1. 注册 Mapper 文件 mybatis-config.xml


<mappers>


<mapper resource="UserDAOMapper.xml"/>


</mappers>


  1. MybatisAPI 调用


public class TestMybatis {


public static void main(String[] args) throws IOException {


InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");


SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);


SqlSession session = sqlSessionFactory.openSession();


UserDAO userDAO = session.getMapper(UserDAO.class);


User user = new User();


user.setName("yusael");


user.setPassword("123456");


userDAO.save(user);


session.commit();


}


}


Mybatis 开发中存在的问题


===================================================================================


问题:配置繁琐、代码冗余


  1. 实体

  2. 实体别名 配置繁琐

  3. 创建 DAO 接口

  4. 实现 Mapper 文件

  5. 注册 Mapper 文件 配置繁琐

  6. Mybatis API 调用 代码冗余


Spring 与 Mybatis 整合思路


========================================================================================




Spring 与 Mybatis 整合的开发步骤


===========================================================================================


  • 配置文件(ApplicationContext.xml)进行相关配置(只需要配置一次

  • 编码


1.实体类


2.表


3.创建 DAO 接口


4.Mapper 文件配置


Spring 与 Mybatis 整合的编码


=========================================================================================


搭建开发环境 pom.xml




<dependency>


<groupId>org.springframework</groupId>


<artifactId>spring-jdbc</artifactId>


<version>5.2.6.RELEASE</version>


</dependency>


<dependency>


<groupId>org.mybatis</groupId>


<artifactId>mybatis-spring</artifactId>


<version>2.0.4</version>


</dependency>


<dependency>


<groupId>com.alibaba</groupId>


<artifactId>druid</artifactId>


<version>1.1.12</version>


</dependency>


<dependency>


<groupId>mysql</groupId>


<artifactId>mysql-connector-java</artifactId>


<version>5.1.43</version>


</dependency>


<dependency>


<groupId>org.mybatis</groupId>


<artifactId>mybatis</artifactId>


<version>3.5.4</version>


</dependency>


Spring 配置文件的配置




<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">


<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">


<property name="driverClassName" value="com.mysql.jdbc.Driver"/>


<property name="url" value="jdbc:mysql://localhost:3306/yus?useSSL=false"/>


<property name="username" value="root"/>


<property name="password" value="1234"/>


</bean>


<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">


<property name="dataSource" ref="dataSource"/>


<property name="typeAliasesPackage" value="com.yusael.entity"/>


<property name="mapperLocations">


<list>


<value>classpath:com.yusael.dao/*Mapper.xml</value>


</list>


</property>


</bean>


<bean id="scanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">


<property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean"/>


<property name="basePackage" value="com.yusael.dao"/>


</bean>


</beans>


编码




  1. 实体 com.yusael.entity.User


public class User implements Serializable {


private Integer id;


private String name;


private String password;


public Integer getId() {


return id;


}


public void setId(Integer id) {


this.id = id;


}


public String getName() {


return name;


}


public void setName(String name) {


this.name = name;


}


public String getPassword() {


return password;


}


public void setPassword(String password) {


this.password = password;


}


}


  1. t_user


create table t_users values (


id int(11) primary key auto_increment,


name varchar(12),


password varchar(12)


);


  1. DAO 接口 com.yusael.dao.UserDAO


public interface UserDAO {


public void save(User user);


}


  1. Mapper 文件配置 resources/applicationContext.xml


<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">


<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">


<property name="driverClassName" value="com.mysql.jdbc.Driver"/>


<property name="url" value="jdbc:mysql://localhost:3306/yus?useSSL=false"/>


<property name="username" value="root"/>


<property name="password" value="1234"/>


</bean>


<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">


<property name="dataSource" ref="dataSource"/>


<property name="typeAliasesPackage" value="com.yusael.entity"/>


<property name="mapperLocations">


<list>


<value>classpath:com.yusael.dao/*Mapper.xml</value>


</list>


</property>


</bean>


<bean id="scanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">


<property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean"/>


<property name="basePackage" value="com.yusael.dao"/>


</bean>


</beans>


  1. 测试


/**


  • 用于测试: Spring 与 Mybatis 的整合


*/


@Test


public void test() {


ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");


UserDAO userDAO = (UserDAO) ctx.getBean("userDAO");


User user = new User();


user.setName("xiaojr");


user.setPassword("999999");


userDAO.save(user);


}


Spring 与 Mybatis 整合细节




问题:Spring 与 Myabatis 整合后,为什么 DAO 不提交事务,但是数据能够插入数据库中?

评论

发布
暂无评论
【Spring 持久层】Spring 与 Mybatis 整合,spring教程极客学院