Spring整合Mybatis案例

案例:添加图书到数据库

这里展示的最终效果是一个jsp页面,所以在pom文件中需要引入javaweb的依赖

需要用到的jar包

        <!--mybatis jar包-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.2.2</version>
        </dependency>

        <!--Mybatis+Spring整合-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.2.1</version>
        </dependency>

        <!-- Spring整合JavaWeb的包 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.2.0.RELEASE</version>
        </dependency>

Dao层:这里整合的是mybatis  所以不需要dao层的实现类

public interface IBookDao {
    public int addBook(book book);
}

IBooDao.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="Dao.IBookDao">
   <insert id="addBook">
        insert  into book(bookName,bookAuther,bookprice) values(#{bookName},#{bookAuther},#{bookprice})
    </insert>
</mapper>

service层:

public interface IBookDaoService {
    public int addBook(book book);
}
public class IBookServiceImpl implements IBookDaoService {
    IBookDao dao;

    public IBookDao getDao() {
        return dao;
    }

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

    public int addBook(book book) {
        return dao.addBook(book);
    }
}

Servlet的web.xml配置:

<web-app>
  <display-name>Archetype Created Web Application</display-name>
    <!--读取到配置文件-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContextSpringMybatis.xml</param-value>
    </context-param>
    <!--监听器-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>IBookServlet</servlet-name>
        <servlet-class>Servlet.IBookServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>IBookServlet</servlet-name>
        <url-pattern>/IBookServlet</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>addbook.jsp</welcome-file>
    </welcome-file-list>
</web-app>

IBookServlet内部代码的书写:

public class IBookServlet extends HttpServlet {
    IBookDaoService service;
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //处理乱码
       request.setCharacterEncoding("utf-8");
       //获取到jsp页面输入文本框的值
        String bookName = request.getParameter("bookName");
        String bookAuther = request.getParameter("bookAuther");
        String bookPrice = request.getParameter("bookPrice");
        //声明一个对象  并把获取到的属性装进去
        book bo=new book();
        bo.setBookName(bookName);
        bo.setBookAuther(bookAuther);
        bo.setbookPrice(Integer.parseInt(bookPrice));
        //识别到配置文件
        ApplicationContext context= WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext());
        //获取配置文件中的service层的Bean的id
        IBookDaoService bean = (IBookDaoService)context.getBean("bookService");
        //调用增加图书的方法
        int i = bean.addBook(bo);
        if(i>0){
            request.getSession().setAttribute("list",i);
            request.getRequestDispatcher("/index.jsp").forward(request,response);
        }else{
            response.sendRedirect(request.getContextPath()+"/addbook.jsp");
        }
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
    public IBookDaoService getService() {
        return service;
    }

    public void setService(IBookDaoService service) {
        this.service = service;
    }
}

appliacationContextSpringMybatis.xml的配置代码:

 //配置数据源
<bean id="dataSources" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="${jdbc.driver}"></property> <property name="url" value="${jdbc.url}"></property> <property name="username" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <!--识别到properties文件一--> <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder> <!读取到大配置文件----> <bean class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSources"></property> <property name="configLocation" value="classpath:mybatis-config.xml"></property> </bean> <!--Dao 层--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="Dao"></property> </bean> <!--service层 由于这里的dao层没有实现类 所以ref写的是接口的名字 会为其自动生成一个实现类
但是命名一定要规范 I开头的接口名字 自动生成的就是I开头的默认实现类 不是I开头的 会生成小写字母开头的实现类
--> <bean id="bookService" class="Service.IBookServiceImpl"> <property name="dao" ref="IBookDao"></property> </bean> <!--事务管理器--> <bean id="transactionManagers" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSources"></property> </bean> <!--注解事务--> <tx:annotation-driven transaction-manager="transactionManagers"></tx:annotation-driven>

最后就是设置好Jsp页面就可以了

 

posted @ 2018-03-18 15:35  风骚少年  阅读(97)  评论(0编辑  收藏  举报
AmazingCounters.com