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# double 四舍五入

    public static double Round(object data) { if (data == null || data == System.DBNull.Value) { return ...

  2. Android开发之Touch事件分发机制

    原地址http://www.cnblogs.com/linjzong/p/4191891.html Touch事件分发中只有两个主角:ViewGroup和View.Activity的Touch事件事实 ...

  3. 浅谈php生成静态页面

    一.引 言 在速度上,静态页面要比动态页面的比方php快很多,这是毫无疑问的,但是由于静态页面的灵活性较差,如果不借助数据库或其他的设备保存相关信息的话,整体的管理上比较繁琐,比方修改编辑.比方阅读权 ...

  4. Activity数据传递

    1.在启动界面里通过intent调用方法putExtra添加欲携带数据 2.在被启动界面里通过getIntent方法获取Intent对象 3.通过intent的getXxxExtra方法获取对应的数据

  5. cosbench read异常解决办法。 Unable to verify integrity of data download. Client calculated content hash didn't match hash calculated by Amazon S3. The data may be corrupt.

    问题:cosbench read测试failed 报错如下 Cosbench v0.4.2.c4 against Ceph (Hammer) / radosgw / HAproxy's HTTP en ...

  6. 模仿iframe框架,由分隔栏动态改变左右两侧div大小———基于jQuery

    <!DOCTYPE html><html lang="zh-cn"><head> <meta charset="utf-8&qu ...

  7. iOS地图 -- 区域监听的实现和小练习

    区域监听用到的方法 [self.mgr startMonitoringForRegion:region]; --> 开启区域监听,没有返回值,在代理方法中得到信息并且处理信息 注:该方法只有用户 ...

  8. Android中Shape的使用

    先看一下文档对Shape Drawable的描述: Shape Drawable An XML file that defines a geometric shape, including color ...

  9. Android基础总结(一)

    Android项目的目录结构 Activity:应用被打开时显示的界面 src:项目代码 R.java:项目中所有资源文件的资源id Android.jar:Android的jar包,导入此包方可使用 ...

  10. 多视图学习(multiview learning)

    多视图学习(multi-view learning) 前期吹牛:今天这一章我们就是来吹牛的,刚开始老板在和我说什么叫多视图学习的时候,我的脑海中是这么理解的:我们在欣赏妹子福利照片的时候,不能只看45 ...