ITPub博客

首页 > IT职业 > IT职场 > Mybatis【Mybatis与Spring整合】

Mybatis【Mybatis与Spring整合】

IT职场 作者:lzg215 时间:2021-09-09 09:10:18 0 删除 编辑
Mybatis第五篇【Mybatis与Spring整合】

既然我们已经学了Mybatis的基本开发了,接下来就是Mybatis与Spring的整合了!

以下使用的是Oracle数据库来进行测试

导入jar包
  • aopalliance.jar
  • asm-3.3.1.jar
  • aspectjweaver.jar
  • c3p0-0.9.1.2.jar
  • cglib-2.2.2.jar
  • commons-logging.jar
  • log4j-1.2.16.jar
  • mybatis-3.1.1.jar
  • mybatis-spring-1.1.1.jar
  • mysql-connector-java-5.1.7-bin.jar
  • ojdbc5.jar
  • org.springframework.aop-3.0.5.RELEASE.jar
  • org.springframework.asm-3.0.5.RELEASE.jar
  • org.springframework.beans-3.0.5.RELEASE.jar
  • org.springframework.context-3.0.5.RELEASE.jar
  • org.springframework.core-3.0.5.RELEASE.jar
  • org.springframework.expression-3.0.5.RELEASE.jar
  • org.springframework.jdbc-3.0.5.RELEASE.jar
  • org.springframework.orm-3.0.5.RELEASE.jar
  • org.springframework.transaction-3.0.5.RELEASE.jar
  • org.springframework.web.servlet-3.0.5.RELEASE.jar
  • org.springframework.web-3.0.5.RELEASE.jar
创建表

create table emps(
  eid number(5) primary key,
  ename varchar2(20),
  esal number(8,2),
  esex varchar2(2)
);
创建实体

package entity;

/**
 * 员工
 * @author AdminTC
 */
public class Emp {
    private Integer id;
    private String name;
    private Double sal;
    private String sex;
    public Emp(){}
    public Emp(Integer id, String name, Double sal, String sex) {
        this.id = id;
        this.name = name;
        this.sal = sal;
        this.sex = sex;
    }
    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 Double getSal() {
        return sal;
    }
    public void setSal(Double sal) {
        this.sal = sal;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
}
创建实体与表的映射文件

<!-- 增加员工 -->
        insert into emps(eid,ename,esal,esex) values(#{id},#{name},#{sal},#{sex})
    
创建Mybatis映射文件配置环境

数据库的信息交由Spring管理!Mybatis配置文件负责加载对应映射文件即可


    
配置Spring核心过滤器【也是加载总配置文件】

    <!-- 核心springmvc核心控制器 -->
    DispatcherServletorg.springframework.web.servlet.DispatcherServletcontextConfigLocationclasspath:spring.xmlDispatcherServlet*.action
配置数据库信息、事务

<!-- 配置C3P0连接池,目的:管理数据库连接 --><!-- 配置SqlSessionFactoryBean,目的:加载mybaits配置文件和映射文件,即替代原Mybatis工具类的作用 --><!-- 配置Mybatis的事务管理器,即因为Mybatis底层用的是JDBC事务管事器,所以在这里依然配置JDBC事务管理器 --><!-- 配置事务通知,即让哪些方法需要事务支持 --><!-- 配置事务切面,即让哪些包下的类需要事务 --><!--扫描注解-->
创建Dao、Service、Action

@Repository
public class EmpDao {
    @Autowired
    private SqlSessionFactory sqlSessionFactory;
    /**
     * 增加员工
     */
    public void add(Emp emp) throws Exception {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        sqlSession.insert("empNamespace.add", emp);
        sqlSession.close();
    }

}
@Service
public class EmpService {

    @Autowired
    private zhongfucheng.dao.EmpDao empDao;
    public void addEmp(Emp emp) throws Exception {
        empDao.add(emp);
    }
}

@Controller
@RequestMapping("/emp")
public class EmpAction {

    @Autowired
    private EmpService empService;

    @RequestMapping("/register")
    public void register(Emp emp) throws Exception {
        empService.addEmp(emp);
        System.out.println("注册成功");
    }

}
JSP页面测试




  
员工注册
编号
姓名
薪水
性别 男 女
总结
  • web.xml加载Spring配置文件
  • Spring配置文件配置数据连接池,SessionFactory、事务、扫描注解
  • Mybatis总配置文件、实体以及相对应的映射文件
  • 将映射文件加入到总配置文件中。

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/420/viewspace-2799807/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2020-07-23

  • 博文量
    0
  • 访问量
    754