zoukankan      html  css  js  c++  java
  • 整合Spring、SpringMVC、MyBatis

    spring+springmvc+mybatis集成

    一个核心:将对象交给spring管理。

    1新建web项目

    2添加项目jar包

     

    spring包见上一篇博客

    3建立项目的目录结构

     

    4完成Mapper的集成

    和mybatis进行集成,交给spring产生Mapper接口的代理对象。

    4.1建立Mapper接口

     1 package org.guangsoft.mapper;
     2 
     3 import java.util.List;
     4 
     5 import org.guangsoft.pojo.Privilege;
     6 
     7 public interface PrivilegeMapper
     8 {
     9     public void addPrivilege(Privilege privilege);
    10     public void deletePrivilege(Privilege privilege);
    11     public void updatePrivilege(Privilege privilege);
    12     public Privilege selectPrivilegeById(Privilege privilege);
    13     public List<Privilege> selectAllPrivileges();
    14 }

    4.2建立Mapper.xml文件

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <!DOCTYPE mapper PUBLIC "-//mybatis.org/DTD Mapper 3.0" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
     3 <mapper namespace="org.guangsoft.mapper.PrivilegeMapper">
     4      <insert id="addPrivilege" parameterType="org.guangsoft.pojo.Privilege">
     5          insert into privilege values(null,#{pname})
     6      </insert>
     7      <delete id="deletePrivilege" parameterType="org.guangsoft.pojo.Privilege">
     8          delete from privilege
     9      </delete>
    10      <delete id="updatePrivilege" parameterType="org.guangsoft.pojo.Privilege">
    11          update privilege set pname=#{pname} where pid=#{pid}
    12      </delete>
    13      <select id="selectPrivilegeById" parameterType="org.guangsoft.pojo.Privilege" resultType="org.guangsoft.pojo.Privilege">
    14          select * from privilege where pid=#{pid}
    15      </select>
    16      <select id="selectAllPrivileges" resultType="org.guangsoft.pojo.Privilege">
    17          select * from privilege
    18      </select>
    19 </mapper> 

    4.3建立application_mapper.xml

    在config下建立:

    配置数据库连接池。

    管理SqlSessionFactory,注入DataSource

    产生Mapper接口的代理对象。

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans
     3     xmlns="http://www.springframework.org/schema/beans"
     4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     5     xmlns:p="http://www.springframework.org/schema/p"
     6     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
     7 
     8     <!-- 数据库连接池 -->
     9     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    10         <!-- 注入数据库连接字符串 -->
    11         <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
    12         <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/test"></property>
    13         <property name="user" value="root"></property>
    14         <property name="password" value="root"></property>
    15     </bean>
    16     <!-- 实例化sessionFactory -->
    17     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    18         <!-- 注入数据库连接池 -->
    19         <property name="dataSource" ref="dataSource"></property>
    20     </bean>
    21     <!-- 产生mapper接口的代理对象 
    22         mapper接口和mapperxml名字必须一样
    23         mapper.java和mapper.xml必须在同一目录下
    24         产生的代理对象id文件Mapper接口的第一个字母小写
    25     -->
    26     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    27         <!-- 注入sessionFactory -->
    28         <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    29         <!-- 注入扫描的包 -->
    30         <property name="basePackage" value="org.guangsoft.mapper"></property>
    31     </bean>
    32 </beans> 

    5完成service的功能

    5.1建立Service接口

     1 package org.guangsoft.service;
     2 
     3 import java.util.List;
     4 
     5 import org.guangsoft.pojo.Privilege;
     6 
     7 public interface PrivilegeService
     8 {
     9     public void addPrivilege(Privilege privilege);
    10     public void deletePrivilege(Privilege privilege);
    11     public void updatePrivilege(Privilege privilege);
    12     public Privilege selectPrivilegeById(Privilege privilege);
    13     public List<Privilege> selectAllPrivileges();
    14

    5.2建立业务接口实现类

    将实现类的对象纳入spring容器。注入Mapper接口的代理对象

     1 package org.guangsoft.service.impl;
     2 
     3 import java.util.List;
     4 
     5 import org.guangsoft.mapper.PrivilegeMapper;
     6 import org.guangsoft.pojo.Privilege;
     7 import org.guangsoft.service.PrivilegeService;
     8 import org.springframework.beans.factory.annotation.Autowired;
     9 import org.springframework.stereotype.Service;
    10 
    11 @Service
    12 public class PrivilegeServiceImpl implements PrivilegeService
    13 {
    14     /**
    15      * 注入Mapper接口的代理对象
    16      */
    17     @Autowired
    18     private PrivilegeMapper privilegeMapper;
    19     
    20     @Override
    21     public void addPrivilege(Privilege privilege)
    22     {
    23         privilegeMapper.addPrivilege(privilege);
    24     }
    25 
    26     @Override
    27     public void deletePrivilege(Privilege privilege)
    28     {
    29         privilegeMapper.deletePrivilege(privilege);
    30     }
    31 
    32     @Override
    33     public void updatePrivilege(Privilege privilege)
    34     {
    35         privilegeMapper.updatePrivilege(privilege);
    36     }
    37 
    38     @Override
    39     public Privilege selectPrivilegeById(Privilege privilege)
    40     {
    41         return privilegeMapper.selectPrivilegeById(privilege);
    42     }
    43 
    44     @Override
    45     public List<Privilege> selectAllPrivileges()
    46     {
    47         return privilegeMapper.selectAllPrivileges();
    48     }
    49 
    50

    5.3建立application_service.xml

    扫描service包

    事务配置

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans
     3     xmlns="http://www.springframework.org/schema/beans"
     4     xmlns:context="http://www.springframework.org/schema/context"
     5     xmlns:tx="http://www.springframework.org/schema/tx"
     6     xmlns:aop="http://www.springframework.org/schema/aop"
     7     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     8     xmlns:p="http://www.springframework.org/schema/p"
     9     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    10     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
    11     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
    12     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
    13 
    14     <!-- 开启service包的扫描 -->
    15     <context:component-scan base-package="org.guangsoft.service.impl"></context:component-scan>
    16         <!-- 配置事务  实例化事务管理器 -->
    17         <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    18             <property name="dataSource" ref="dataSource"></property>
    19         </bean>
    20         <!-- 配置切面 -->
    21         <tx:advice id="txAdvice" transaction-manager="transactionManager">
    22             <tx:attributes>
    23                 <tx:method name="add*" isolation="DEFAULT" propagation="REQUIRED"/>
    24             </tx:attributes>
    25         </tx:advice>
    26         <aop:config>
    27             <aop:pointcut expression="execution(* org.guangsoft.service.impl.*.*(..))" id="pc"/>
    28             <aop:advisor advice-ref="txAdvice" pointcut-ref="pc"/>
    29         </aop:config>
    30 </beans> 

    6完成Action的功能

    6.1建立Handler处理器

     1 package org.guangsoft.controller;
     2 
     3 import org.guangsoft.pojo.Privilege;
     4 import org.guangsoft.service.PrivilegeService;
     5 import org.springframework.beans.factory.annotation.Autowired;
     6 import org.springframework.stereotype.Controller;
     7 import org.springframework.web.bind.annotation.RequestMapping;
     8 
     9 @Controller
    10 public class PrivilegeController
    11 {
    12     @Autowired
    13     private PrivilegeService privilegeService;
    14     //定义菜单项求求的方法
    15     @RequestMapping("/addPrivilege")
    16     public String addPrivilege(Privilege privilege)
    17     {
    18         privilegeService.addPrivilege(privilege);
    19         return "index.jsp";
    20     }
    21 }

    6.2建立springmvc的配置文件

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans
     3     xmlns="http://www.springframework.org/schema/beans"
     4     xmlns:context="http://www.springframework.org/schema/context"
     5     xmlns:mvc="http://www.springframework.org/schema/mvc"
     6     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     7     xmlns:p="http://www.springframework.org/schema/p"
     8     xsi:schemaLocation=
     9     "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    10     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
    11     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
    12 
    13     <!-- 开启扫描注解 -->
    14     <context:component-scan base-package="org.guangsoft.controller"></context:component-scan>
    15     <!-- 开启映射注解和适配注解 -->
    16     <mvc:annotation-driven></mvc:annotation-driven>
    17 </beans> 

    7配置web.xml

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
     3   <context-param>
     4     <param-name>contextConfigLocation</param-name>
     5     <param-value>classpath:application_*.xml</param-value>
     6   </context-param>
     7   <listener>
     8     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
     9   </listener>
    10   <servlet>
    11     <servlet-name>springmvc</servlet-name>
    12     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    13     <init-param>
    14       <param-name>contextConfigLocation</param-name>
    15       <param-value>classpath:springmvc_servlet.xml</param-value>
    16     </init-param>
    17     <load-on-startup>1</load-on-startup>
    18   </servlet>
    19   <servlet-mapping>
    20     <servlet-name>springmvc</servlet-name>
    21     <url-pattern>*.action</url-pattern>
    22   </servlet-mapping>
    23 </web-app> 

    8建立视图页面

     1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
     2 <%
     3 String path = request.getContextPath();
     4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
     5 %>
     6 
     7 <!DOCTYPE HTML>
     8 <html>
     9   <head>
    10     <base href="<%=basePath%>">
    11     <title>My JSP 'index.jsp' starting page</title>
    12     <meta http-equiv="pragma" content="no-cache">
    13     <meta http-equiv="cache-control" content="no-cache">
    14   </head>
    15   
    16   <body>
    17       <div align="center">
    18       <form action="addPrivilege.action" method="post">
    19           <div>pname:<input name="pname"  /></div>
    20           <div><input type="submit" value="提交" /></div>
    21       </form>
    22       </div>
    23   </body>
    24 </html> 

    9发布测试

     

  • 相关阅读:
    [BZOJ 1095] [ZJOI 2007]Hide 捉迷藏
    [BZOJ 2759] 一个动态树好题
    BZOJ 3122 SDOI2013 随机数生成器
    [NOIP集训]10月18日
    [NOIP集训]10月17日
    [NOIP集训]10月16日
    [NOI题库]1.3编程基础之算术表达式与顺序执行 题解(一)
    [NOI题库]1.2编程基础之变量定义、赋值及转换 题解
    [NOI题库]1.1编程基础之输入输出 题解
    [作业]排序算法练习(二)
  • 原文地址:https://www.cnblogs.com/guanghe/p/6189074.html
Copyright © 2011-2022 走看看