整合Spring、SpringMVC、MyBatis
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的更多相关文章
- 使用maven整合spring+springmvc+mybatis
使用maven整合spring+springmvc+mybatis 开发环境: 1. jdk1.8 2. eclipse4.7.0 (Oxygen) 3. mysql 5.7 在pom.xml文件中, ...
- maven项目快速搭建SSM框架(一)创建maven项目,SSM框架整合,Spring+Springmvc+Mybatis
首先了解服务器开发的三层架构,分配相应的任务,这样就能明确目标,根据相应的需求去编写相应的操作. 服务器开发,大致分为三层,分别是: 表现层 业务层 持久层 我们用到的框架分别是Spring+Spri ...
- eclipse整合spring+springMVC+Mybatis
一.新建Maven项目 点击菜单栏File项,选择New->Project,选中Maven Project,如下图: 二.配置pom.xml <?xml version="1.0 ...
- SSM框架整合(Spring+SpringMVC+Mybatis)
第一步:创建maven项目并完善项目结构 第二步:相关配置 pom.xml 引入相关jar包 1 <properties> 2 <project.build.sourceEncod ...
- 使用IDEA的gradle整合spring+springmvc+mybatis 采用javaconfig配置
1.在上篇博客里讲述了spring+mybatis的整合,这边在上篇的基础上进行开发. 上篇博客链接http://www.cnblogs.com/huangyichun/p/6149946.html ...
- 整合spring+springmvc+mybatis
开发环境: jdk 1.8 eclipse 4.7.0 (Oxygen) tomcat 8.5.29 mysql 5.7 开发前准备: spring 框架的jar包,在这里使用的是spring-5.0 ...
- shiro与Web项目整合-Spring+SpringMVC+Mybatis+Shiro(八)
Jar包
- Spring+SpringMVC+MyBatis+easyUI整合
进阶篇 Spring+SpringMVC+MyBatis+easyUI整合进阶篇(一)设计一套好的RESTful API 优化篇 Spring+SpringMVC+MyBatis+easyUI整合优化 ...
- Spring+SpringMVC+MyBatis+easyUI整合优化篇
优化篇 Spring+SpringMVC+MyBatis+easyUI整合优化篇(一)System.out.print与Log Spring+SpringMVC+MyBatis+easyUI整合优化篇 ...
- Spring+SpringMVC+MyBatis整合(easyUI、AdminLte3)
实战篇(付费教程) 花了几天的时间,做了一个网站小 Demo,最终效果也与此网站类似.以下是这次实战项目的 Demo 演示. 登录页: 富文本编辑页: 图片上传: 退出登录: SSM 搭建精美实用的管 ...
随机推荐
- greenDao:操作数据库的开源框架
greenDAO: Android ORM for your SQLite database 1. greenDao库获取 英文标题借鉴的是greendrobot官网介绍greenDao时给出的Tit ...
- Windows phone应用开发[21]-图片性能优化
在windows phone 中常在列表中会常包含比较丰富文字和图片混排数据信息. 针对列表数据中除了谈到listbox等控件自身数据虚拟化问题外.虽然wp硬件设备随着SDK 8.0 发布得到应用可使 ...
- django request对象和HttpResponse对象
HttpRequest对象(除非特殊说明,所有属性都是只读,session属性是个例外)HttpRequest.scheme 请求方案(通常为http或https)HttpRequest.body 字 ...
- 【转载】浅谈HTTP中Get与Post的区别
[转载]http://www.cnblogs.com/hyddd/ Http定义了与服务器交互的不同方法,最基本的方法有4种,分别是GET,POST,PUT,DELETE.URL全称是资源描述符,我们 ...
- PyChram中同目录下import引包报错的解决办法?
相信很多同学和我一样在PyChram工具中新建python项目的同目录下import引包会报错提示找不到,这是因为该项目找不到python的环境导致的: 如果文件开始的时候包引包的错误可以,都可以用用 ...
- C++ 顺序容器基础知识总结
0.前言 本文简单地总结了STL的顺序容器的知识点.文中并不涉及具体的实现技巧,对于细节的东西也没有提及.一来不同的标准库有着不同的实现,二来关于具体实现<STL源码剖析>已经展示得全面细 ...
- bzoj 1305 dance跳舞
最大流. 首先二分答案,问题转化为x首舞曲是否可行. 考虑建图,对每个人建立三个点,分别表示全体,喜欢和不喜欢. 源点向每个男生全体点连一条容量为x的边. 每个男生整体点向喜欢点连一条容量为正无穷的边 ...
- LNMP源码编译安装(centos7+nginx1.9+mysql5.6+php7)
1.准备工作: 1)把所有的软件安装在/Data/apps/,源码包放在/Data/tgz/,数据放在/Data/data,日志文件放在/Data/logs,项目放在/Data/webapps, mk ...
- Alpha阶段第七次Scrum Meeting
情况简述 Alpha阶段第七次Scrum Meeting 敏捷开发起始时间 2016/10/28 00:00 敏捷开发终止时间 2016/10/29 00:00 会议基本内容摘要 跟助教进行了交流,明 ...
- [bigdata] 启动CM出现 “JDBC Driver class not found: com.mysql.jdbc.Driver” 以及“Error creating bean with name 'serverLogFetcherImpl'”问题的解决方法
问题:“JDBC Driver class not found: com.mysql.jdbc.Driver” 通过以下命令启动cm [root@hadoop1 ~]# /etc/init.d/cl ...