spring+springmvc+mybatis集成

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

1新建web项目

2添加项目jar包

spring包见上一篇博客

3建立项目的目录结构

4完成Mapper的集成

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

4.1建立Mapper接口

 package org.guangsoft.mapper;

 import java.util.List;

 import org.guangsoft.pojo.Privilege;

 public interface PrivilegeMapper
{
public void addPrivilege(Privilege privilege);
public void deletePrivilege(Privilege privilege);
public void updatePrivilege(Privilege privilege);
public Privilege selectPrivilegeById(Privilege privilege);
public List<Privilege> selectAllPrivileges();
}

4.2建立Mapper.xml文件

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org/DTD Mapper 3.0" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.guangsoft.mapper.PrivilegeMapper">
<insert id="addPrivilege" parameterType="org.guangsoft.pojo.Privilege">
insert into privilege values(null,#{pname})
</insert>
<delete id="deletePrivilege" parameterType="org.guangsoft.pojo.Privilege">
delete from privilege
</delete>
<delete id="updatePrivilege" parameterType="org.guangsoft.pojo.Privilege">
update privilege set pname=#{pname} where pid=#{pid}
</delete>
<select id="selectPrivilegeById" parameterType="org.guangsoft.pojo.Privilege" resultType="org.guangsoft.pojo.Privilege">
select * from privilege where pid=#{pid}
</select>
<select id="selectAllPrivileges" resultType="org.guangsoft.pojo.Privilege">
select * from privilege
</select>
</mapper> 

4.3建立application_mapper.xml

在config下建立:

配置数据库连接池。

管理SqlSessionFactory,注入DataSource

产生Mapper接口的代理对象。

 <?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> <!-- 数据库连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 注入数据库连接字符串 -->
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/test"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
</bean>
<!-- 实例化sessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入数据库连接池 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 产生mapper接口的代理对象
mapper接口和mapperxml名字必须一样
mapper.java和mapper.xml必须在同一目录下
产生的代理对象id文件Mapper接口的第一个字母小写
-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 注入sessionFactory -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
<!-- 注入扫描的包 -->
<property name="basePackage" value="org.guangsoft.mapper"></property>
</bean>
</beans> 

5完成service的功能

5.1建立Service接口

 package org.guangsoft.service;

 import java.util.List;

 import org.guangsoft.pojo.Privilege;

 public interface PrivilegeService
{
public void addPrivilege(Privilege privilege);
public void deletePrivilege(Privilege privilege);
public void updatePrivilege(Privilege privilege);
public Privilege selectPrivilegeById(Privilege privilege);
public List<Privilege> selectAllPrivileges();

5.2建立业务接口实现类

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

 package org.guangsoft.service.impl;

 import java.util.List;

 import org.guangsoft.mapper.PrivilegeMapper;
import org.guangsoft.pojo.Privilege;
import org.guangsoft.service.PrivilegeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; @Service
public class PrivilegeServiceImpl implements PrivilegeService
{
/**
* 注入Mapper接口的代理对象
*/
@Autowired
private PrivilegeMapper privilegeMapper; @Override
public void addPrivilege(Privilege privilege)
{
privilegeMapper.addPrivilege(privilege);
} @Override
public void deletePrivilege(Privilege privilege)
{
privilegeMapper.deletePrivilege(privilege);
} @Override
public void updatePrivilege(Privilege privilege)
{
privilegeMapper.updatePrivilege(privilege);
} @Override
public Privilege selectPrivilegeById(Privilege privilege)
{
return privilegeMapper.selectPrivilegeById(privilege);
} @Override
public List<Privilege> selectAllPrivileges()
{
return privilegeMapper.selectAllPrivileges();
} } 

5.3建立application_service.xml

扫描service包

事务配置

 <?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd"> <!-- 开启service包的扫描 -->
<context:component-scan base-package="org.guangsoft.service.impl"></context:component-scan>
<!-- 配置事务 实例化事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置切面 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" isolation="DEFAULT" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut expression="execution(* org.guangsoft.service.impl.*.*(..))" id="pc"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pc"/>
</aop:config>
</beans> 

6完成Action的功能

6.1建立Handler处理器

 package org.guangsoft.controller;

 import org.guangsoft.pojo.Privilege;
import org.guangsoft.service.PrivilegeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; @Controller
public class PrivilegeController
{
@Autowired
private PrivilegeService privilegeService;
//定义菜单项求求的方法
@RequestMapping("/addPrivilege")
public String addPrivilege(Privilege privilege)
{
privilegeService.addPrivilege(privilege);
return "index.jsp";
}
}

6.2建立springmvc的配置文件

 <?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation=
"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"> <!-- 开启扫描注解 -->
<context:component-scan base-package="org.guangsoft.controller"></context:component-scan>
<!-- 开启映射注解和适配注解 -->
<mvc:annotation-driven></mvc:annotation-driven>
</beans> 

7配置web.xml

 <?xml version="1.0" encoding="UTF-8"?>
<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">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:application_*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc_servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
</web-app> 

8建立视图页面

 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML>
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
</head> <body>
<div align="center">
<form action="addPrivilege.action" method="post">
<div>pname:<input name="pname" /></div>
<div><input type="submit" value="提交" /></div>
</form>
</div>
</body>
</html> 

9发布测试

整合Spring、SpringMVC、MyBatis的更多相关文章

  1. 使用maven整合spring+springmvc+mybatis

    使用maven整合spring+springmvc+mybatis 开发环境: 1. jdk1.8 2. eclipse4.7.0 (Oxygen) 3. mysql 5.7 在pom.xml文件中, ...

  2. maven项目快速搭建SSM框架(一)创建maven项目,SSM框架整合,Spring+Springmvc+Mybatis

    首先了解服务器开发的三层架构,分配相应的任务,这样就能明确目标,根据相应的需求去编写相应的操作. 服务器开发,大致分为三层,分别是: 表现层 业务层 持久层 我们用到的框架分别是Spring+Spri ...

  3. eclipse整合spring+springMVC+Mybatis

    一.新建Maven项目 点击菜单栏File项,选择New->Project,选中Maven Project,如下图: 二.配置pom.xml <?xml version="1.0 ...

  4. SSM框架整合(Spring+SpringMVC+Mybatis)

    第一步:创建maven项目并完善项目结构  第二步:相关配置 pom.xml 引入相关jar包 1 <properties> 2 <project.build.sourceEncod ...

  5. 使用IDEA的gradle整合spring+springmvc+mybatis 采用javaconfig配置

    1.在上篇博客里讲述了spring+mybatis的整合,这边在上篇的基础上进行开发. 上篇博客链接http://www.cnblogs.com/huangyichun/p/6149946.html ...

  6. 整合spring+springmvc+mybatis

    开发环境: jdk 1.8 eclipse 4.7.0 (Oxygen) tomcat 8.5.29 mysql 5.7 开发前准备: spring 框架的jar包,在这里使用的是spring-5.0 ...

  7. shiro与Web项目整合-Spring+SpringMVC+Mybatis+Shiro(八)

    Jar包

  8. Spring+SpringMVC+MyBatis+easyUI整合

    进阶篇 Spring+SpringMVC+MyBatis+easyUI整合进阶篇(一)设计一套好的RESTful API 优化篇 Spring+SpringMVC+MyBatis+easyUI整合优化 ...

  9. Spring+SpringMVC+MyBatis+easyUI整合优化篇

    优化篇 Spring+SpringMVC+MyBatis+easyUI整合优化篇(一)System.out.print与Log Spring+SpringMVC+MyBatis+easyUI整合优化篇 ...

  10. Spring+SpringMVC+MyBatis整合(easyUI、AdminLte3)

    实战篇(付费教程) 花了几天的时间,做了一个网站小 Demo,最终效果也与此网站类似.以下是这次实战项目的 Demo 演示. 登录页: 富文本编辑页: 图片上传: 退出登录: SSM 搭建精美实用的管 ...

随机推荐

  1. C#的前世今生,学会C#还能找到高薪工作吗?

    其实C#,.net正在逐步淡出程序员的视野是正在发生的现实,量子及量子的小伙伴们,还在坚持写C#代码的人几乎没有了,回忆起过去那些写C#时候的美好时光,真是不胜唏嘘,最近园子里的一篇<C#程序员 ...

  2. VSS每次打开都需要服务器账号和密码的解决方法

    最近在做的一个项目还在使用非常非常古老的VSS(Microsoft Visual SourceSafe)2005,是的,没有看错,是VSS2005,而不是Git或是SVN.然后我的VSS在安装过后遇到 ...

  3. jQuery之Ajax--底层接口

    1. $.ajax()方法:是jQuery最底层的Ajax实现.它的结构为:$.ajax(options).该方法只有一个参数,但在这个对象里面包含了$.ajax()方法所需要的请求设置以及回调函数等 ...

  4. redis 的源码编译安装

    首先我们下载软件包到指定的目录下 tar -zxvf redis-2.8.19.tar.gz cd redis-2.8.19 make make PREFIX=/usr/local/redis ins ...

  5. [uva11722&&cogs1488]和朋友会面Joining with Friend

    几何概型,<训练指南>的题.分类讨论太神啦我不会,我只会萌萌哒的simpson强上~这里用正方形在y=x-w的左上方的面积减去在y=x+w左上方的面积就是两条直线之间的面积,然后切出来的每 ...

  6. qthread 使用 signal 方法通信

    因为之间尝试过的 signal 机制,都是在 emit singnal_my() 的地方,直接调用了 slot 函数:相当于,slot 只是一个回调函数. 所以,在这里有点困惑,如果是要顺序执行完 s ...

  7. ajax之 get post请求

    get请求 function get(){ $.get( "./Aservlet?id=5", function(data, textStatus, jqXHR){ $(" ...

  8. PHPstorm的数据库功能

    PHPstorm真是神器,居然有表.视图.存储过程的功能,非常人性化,建表那叫一个舒服,而且sql语句可以像其他代码一样显示"区域",结构更加清晰.

  9. ng-switch

    <p>ng-switch : </p> <div ng-switch="isShow"><!--isShow是boolean值--> ...

  10. 第二轮冲刺-Runner站立会议03

    今天做了什么:查看gridview与baseadapter适配器 明天准备做什么:继续gridview与baseadapter适配器 遇到的困难:暂无