首页 技术 正文
技术 2022年11月15日
0 收藏 323 点赞 4,855 浏览 8384 个字

spring+springmvc+mybatis集成

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

1新建web项目

2添加项目jar包

整合Spring、SpringMVC、MyBatis

spring包见上一篇博客

3建立项目的目录结构

整合Spring、SpringMVC、MyBatis

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%>" rel="external nofollow" >
<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发布测试

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,991
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,506
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,349
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,134
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,766
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,844