Spring整合MyBatis(含监听器)

    Spring整合MyBatis首先要下载一个jar包:

        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webartifactId>
            <version>4.3.9.RELEASEversion>
        dependency>


<dependency>
    <groupId>org.mybatisgroupId>
    <artifactId>mybatis-springartifactId>
    <version>1.2.0version>
dependency>


<dependency>
    <groupId>org.mybatisgroupId>
    <artifactId>mybatisartifactId>
    <version>3.2.2version>
dependency>


<dependency>
    <groupId>mysqlgroupId>
    <artifactId>mysql-connector-javaartifactId>
    <version>5.1.31version>
dependency>

先建一个文件,在该文件中创建实体类eitity层,dao层,servic层,servlet层

首先在entity层中,创建一个Book实体类,并写上该类的属性值,将其进行get,set封装

public class Book {
    private Integer bookId;
    private String bookName;
    private int bookPrice;
之后就在dao层中创建一个BookDao接口,在该接口中写上一个实现方法。

   public interface BookDao {

           //添加

    public  int  add(Book book);
}
然后就该配置小配置BookDao.xml,当然也是在dao层,在这里需要注意的是接口名与小配置的名一致
xml version="1.0" encoding="UTF-8" ?>

 mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.happy.SpringSSM.dao.BookDao">
    <insert id="add">    //id是指接口中的实现方法名
        insert into book(bookname,bookprice) values(#{bookName},#{bookPrice})
    insert>
mapper>
接下来就到了service层了,在该层中也需要创建一个接口,该接口内中也要写上一个实现方法,该方法和dao层中的方法一致。
public interface BookService {
    public  int add(Book book);
}
接口写完之后,我们也需要在该层中写上一个BookServiceImpl类,在该类中实现BookService接口中的方法,并植入BookDao,将其进行封装
public class BookServiceImpl implements BookService {
      BookDao dao;

    public BookDao getDao() {
        return dao;
    }

    public void setDao(BookDao dao) {
        this.dao = dao;
    }

    public int add(Book book) {
        return dao.add(book);
    }
}
要想在页面中显示出来效果,重要的就靠servlet层中的了,当然在创建Servlet的时候一定要引包噢

<dependency>
    <groupId>javaeegroupId>
    <artifactId>javaee-apiartifactId>
    <version>5version>
dependency>


<dependency>
    <groupId>javax.servletgroupId>
    <artifactId>jstlartifactId>
    <version>1.2version>
    <scope>runtimescope>
dependency>
Spring整合MyBatis(含监听器)_第1张图片
  接下来就到了很重要的环节了,配置阶段,在配置的时候会需要很多jar包,首先我们要先下载需要的jar包,将其放在pom.xml中   

<dependency>
    <groupId>org.springframeworkgroupId>
    <artifactId>spring-beansartifactId>
    <version>4.2.3.RELEASEversion>
dependency>

<dependency>
    <groupId>org.springframeworkgroupId>
    <artifactId>spring-contextartifactId>
    <version>4.2.2.RELEASEversion>
dependency>

<dependency>
    <groupId> org.aspectjgroupId >
    <artifactId> aspectjweaverartifactId >
    <version> 1.8.7version >
dependency>    

<dependency>
    <groupId>org.springframeworkgroupId>
    <artifactId>spring-jdbcartifactId>
    <version>3.2.13.RELEASEversion>
dependency>

<dependency>
    <groupId>org.springframeworkgroupId>
    <artifactId>spring-txartifactId>
    <version>3.2.13.RELEASEversion>
dependency>

<dependency>
    <groupId>commons-dbcpgroupId>
    <artifactId>commons-dbcpartifactId>
    <version>1.3version>
dependency>

    
    <dependency>
        <groupId>c3p0groupId>
        <artifactId>c3p0artifactId>
        <version>0.9.1.2version>
    dependency>
    
    <dependency>
        <groupId>com.alibabagroupId>
        <artifactId>druidartifactId>
        <version>1.0.29version>
    dependency>
MyBatis-config.xml
xml version="1.0" encoding="UTF-8" ?>
 configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

    
    <typeAliases>
        <package name="cn.happy.SpringSSM.entity">package>
    typeAliases>

configuration>
jdbc.properties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///bookstok
jdbc.user=sha
jdbc.password=sha
applicationContextSpringSSM.xml
xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:p="http://www.springframework.org/schema/p"
       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

        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.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
     ">
    
    <context:property-placeholder location="classpath:jdbc.properties">context:property-placeholder>
    
  

    
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
        <property name="driverClassName" value="${jdbc.driverClassName}">property>
        <property name="url" value="${jdbc.url}">property>
        <property name="username" value="${jdbc.user}" >property>
        <property name="password" value="${jdbc.password}" >property>
    bean>
    
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        
        <property name="dataSource" ref="dataSource">property>
        
        <property name="configLocation" value="classpath:MyBatis-config.xml">property>
    bean>

  <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="cn.happy.SpringSSM.dao">property>
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory">property>
bean>
        
    <bean id="bookservice" class="cn.happy.SpringSSM.service.BookServiceImpl">
        <property name="dao" ref="bookDao">property>
    bean>

    
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource">property>
    bean>


    
beans>
在创建一个bookadd.jsp页面
Spring整合MyBatis(含监听器)_第2张图片
  切记不要忘了在web.xml中补全相关的信息
  Spring整合MyBatis(含监听器)_第3张图片
   

你可能感兴趣的:(Spring整合MyBatis(含监听器))