Mybatis與Spring整合

Mybatis與Spring整合

既然咱們已經學了Mybatis的基本開發了,接下來就是Mybatis與Spring的整合了!html

如下使用的是Oracle數據庫來進行測試java

導入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;
    }
}

建立實體與表的映射文件

<?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="empNamespace">
    
    <resultMap type="entity.Emp" id="empMap">
        <id property="id" column="eid"/>
        <result property="name" column="ename"/>
        <result property="sal" column="esal"/>
        <result property="sex" column="esex"/>
    </resultMap>    
    
    <!-- 增長員工 -->
    <insert id="add" parameterType="entity.Emp">
        insert into emps(eid,ename,esal,esex) values(#{id},#{name},#{sal},#{sex})
    </insert>
    
</mapper>

建立Mybatis映射文件配置環境

數據庫的信息交由Spring管理!Mybatis配置文件負責加載對應映射文件便可mysql

<mappers>
        <mapper resource="zhongfucheng/entity/EmpMapper.xml"/>

    </mappers>
</configuration>

配置Spring核心過濾器【也是加載總配置文件】

<!-- 核心springmvc核心控制器 -->
    <servlet>
        <servlet-name>DispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>DispatcherServlet</servlet-name>
        <url-pattern>*.action</url-pattern>
    </servlet-mapping>

配置數據庫信息、事務

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">


    <!-- 配置C3P0鏈接池,目的:管理數據庫鏈接 -->
    <bean id="comboPooledDataSourceID" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="oracle.jdbc.driver.OracleDriver"/>
        <property name="jdbcUrl" value="jdbc:oracle:thin:@127.0.0.1:1521:ZHONGFUCHENG"/>
        <property name="user" value="scott"/>
        <property name="password" value="tiger"/>
    </bean>


    <!-- 配置SqlSessionFactoryBean,目的:加載mybaits配置文件和映射文件,即替代原Mybatis工具類的做用 -->
    <bean id="sqlSessionFactoryBeanID" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="configLocation" value="classpath:mybatis.xml"/>
        <property name="dataSource" ref="comboPooledDataSourceID"/>
    </bean>


    <!-- 配置Mybatis的事務管理器,即由於Mybatis底層用的是JDBC事務管事器,因此在這裏依然配置JDBC事務管理器 -->
    <bean id="dataSourceTransactionManagerID" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="comboPooledDataSourceID"/>
    </bean>

    <!-- 配置事務通知,即讓哪些方法須要事務支持 -->
    <tx:advice id="tx" transaction-manager="dataSourceTransactionManagerID">
        <tx:attributes>
            <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>

    <!-- 配置事務切面,即讓哪些包下的類須要事務 -->
    <aop:config>
        <aop:pointcut id="pointcut" expression="execution(* zhongfucheng.service.*.*(..))"/>
        <aop:advisor advice-ref="tx" pointcut-ref="pointcut"/>
    </aop:config>

    <!--掃描註解-->
    <context:component-scan base-package="zhongfucheng"/>


</beans>

建立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頁面測試

<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>員工註冊</title>
  </head>
  <body>
    <form action="${pageContext.request.contextPath}/emp/register.action" method="POST">
        <table border="2" align="center">
            <tr>
                <th>編號</th>
                <td><input type="text" name="id"></td>
            </tr>
            <tr>
                <th>姓名</th>
                <td><input type="text" name="name"></td>
            </tr>
            <tr>
                <th>薪水</th>
                <td><input type="text" name="sal"></td>
            </tr>
            <tr>
                <th>性別</th>
                <td>
                    <input type="radio" name="sex" value="男"/>男
                    <input type="radio" name="sex" value="女" checked/>女
                </td>
            </tr>
            <tr>
                <td colspan="2" align="center">
                    <input type="submit" value="註冊"/>
                </td>
            </tr>
        </table>
    </form>     
  </body>
</html>

總結

  • web.xml加載Spring配置文件
  • Spring配置文件配置數據鏈接池,SessionFactory、事務、掃描註解
  • Mybatis總配置文件、實體以及相對應的映射文件
  • 將映射文件加入到總配置文件中。

若是文章有錯的地方歡迎指正,你們互相交流。習慣在微信看技術文章,想要獲取更多的Java資源的同窗,能夠關注微信公衆號:Java3yweb

相關文章
相關標籤/搜索