整合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 搭建精美实用的管 ...
随机推荐
- mariadb
MariaDB数据库管理系统是MySQL的一个分支
- Debian8搭建php环境
安装apache 新装的系统发现 apt-get install apach<tab> 没有自动补全 请查看 这里 apt-get install apache2 安装mysql apt- ...
- 如何用命令检查Linux服务器性能
1.查看系统负载 (1)uptime 这个命令可以快速查看机器的负载情况. 在Linux系统中,这些数据表示等待CPU资源的进程和阻塞在不可中断IO进程(进程状态为D)的数量. 命令的输出,load ...
- js轮播(qq幻灯片效果)
<!DOCTYPE html><html><head><meta charset="utf-8"><meta http-equ ...
- React.js入门笔记(续):用React的方式来思考
本文主要内容来自React官方文档中的"Thinking React"部分,总结算是又一篇笔记.主要介绍使用React开发组件的官方思路.代码内容经笔者改写为较熟悉的ES5语法. ...
- webpack-dev-server
webpack-dev-server是一个小型的node.js Express服务器,它使用webpack-dev-middleware中间件来为通过webpack打包生成的资源文件提供Web服务.它 ...
- git学习之旅
http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/0013743256916071d ...
- JVM Management API
JVM本身提供了一组管理的API,通过该API,我们可以获取得到JVM内部主要运行信息,包括内存各代的数据.JVM当前所有线程及其栈相关信 息等等.各种JDK自带的剖析工具,包括jps.jstack. ...
- [Search Engine] 搜索引擎技术之查询处理
我们之前从开发者的角度谈了一些有关搜索引擎的技术,其实对于用户来说,我们不需要知道网络爬虫到底是怎样爬取网页的,也不需要知道倒排索引是什么,我们只需要输入我们的查询词query,然后能够得到我们想要的 ...
- maven项目常见问题
问题1:Maven项目,右键-update project后报错如下的解决办法: 1).DescriptionResourcePathLocationType Java compiler level ...