JavaWeb——springMVC、mybatis与spring的整合
一、引言
距离上次写javaweb系列已经过去快两三个月了,由于时间冲突和任务紧急程度原因没来得及弄完,这次补上。本文使用了springMVC+spring+mybatis框架,在前面学习springmvc和mybatis的基础上整合了spring框架,并使用到项目中。这需要我们有一些spring的基本了解,比如控制反转、依赖注入等概念==
二、整体思路
主要是在web.xml中配置前端控制器和一些其他的注入配置,上图把层级关系列的很清晰了==
三、代码
文件目录结构图如下(源码下载地址点击打开链接):
1、src文件
com.xcy.po中User类:
package com.xcy.po;public class User {private int F_ID;private String F_CODE;private String F_PW;public int getF_ID() {return F_ID;}public void setF_ID(int f_ID) {F_ID = f_ID;}public String getF_CODD() {return F_CODE;}public void setF_CODE(String f_CODE) {F_CODE = f_CODE;}public String getF_PW() {return F_PW;}public void setF_PW(String f_PW) {F_PW = f_PW;}@Overridepublic String toString() {return "Bike [F_ID=" + F_ID + ", F_CODD=" + F_CODE + ", F_PW=" + F_PW + "]";}}
com.xcy.mapper中UserMapper接口
package com.xcy.mapper;import com.xcy.po.User;public interface UserMapper {public User findUser (int id) throws Exception;
}
com.xcy.mapper中UserMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xcy.mapper.UserMapper"><select id="findUser" parameterType="int" resultType="com.xcy.po.User">select * from T_BIKE where F_ID = #{id}</select>
</mapper>
com.xcy.service中UserService:
package com.xcy.service;import com.xcy.po.User;public interface UserService {public User findUser (int i) throws Exception;
}
com.xcy.service中UserServiceImpl
package com.xcy.service;import org.springframework.beans.factory.annotation.Autowired;import com.xcy.mapper.UserMapper;
import com.xcy.po.User;public class UserServiceImpl implements UserService{@Autowiredprivate UserMapper userMapper;@Overridepublic User findUser(int i) throws Exception {// TODO Auto-generated method stubreturn userMapper.findUser(i);}}
com.xcy.controller中UserController
package com.xcy.controller;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;import com.xcy.po.User;
import com.xcy.service.UserServiceImpl;import sun.launcher.resources.launcher;@Controller
public class UserController {
@Autowired
private UserServiceImpl userServiceImpl;@RequestMapping("/getUser")@ResponseBodypublic User getUser() throws Exception {User user=userServiceImpl.findUser(1);return user;}
}
2、配置文件
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"> <display-name>Yellowbike</display-name> <!-- 加载spring容器 -->
<listener><listener-class> org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param><param-name>contextConfigLocation</param-name><param-value>WEB-INF/classes/spring/applicationContext-*.xml</param-value>
</context-param>
<!--springmvc前端控制器 --><servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/springmvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
</web-app>
springmvc.xml,配置前端控制器
<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!-- <bean name="/getName" class="com.xcy.controller.UserController"></bean> <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean> <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean> --> <!--配置handler --> <context:component-scan base-package="com.xcy.controller"></context:component-scan> <!--加载handlermapping和handleradapter --> <mvc:annotation-driven></mvc:annotation-driven> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"></property> <property name="suffix" value=".jsp"></property> </bean>
</beans>
applicationContext-dao.xml,其中引用了sqlMapConfig配置文件
<?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:mvc="http://www.springframework.org/schema/mvc"xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"><!--加载配置文件 --><context:property-placeholder location="classpath:db.properties" /><!--dbcp数据源 --><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close"><property name="driverClassName" value="${jdbc.driver}" /><property name="url" value="${jdbc.url}" /><property name="username" value="${jdbc.username}" /><property name="password" value="${jdbc.password}" /><!--maxActive: 最大连接数量 --><property name="maxActive" value="150" /><!--maxIdle: 最大空闲连接 --><property name="maxIdle" value="20" /></bean><!-- 注入sqlsessionfactory --><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml" /><property name="dataSource" ref="dataSource" /></bean><!-- 配置mapperfactoryBean -->
<!-- <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"><property name="mapperInterface" value="com.xcy.mapper.UserMapper" /><property name="sqlSessionFactory" ref="sqlSessionFactory" /></bean> --><!--mapper扫描,扫出mapper接口,自动注入spring --><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="com.xcy.mapper"/><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/></bean>
</beans>
sqlMapConfig.xml,其中可以配置setting,alias,扫描mapper(applicationContext-dao中已配置可以免去),这里没有配置啥==
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration> <!-- setting -->
<!-- alias -->
<!--可以不需要配置,mybatis—spring扫描 -->
<!-- <mappers> <package name="com.xcy.mapper"/> </mappers> -->
</configuration>
applicationContext-service.xml,注入service实现类
<?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:mvc="http://www.springframework.org/schema/mvc"xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"><bean id="userServiceImpl" class="com.xcy.service.UserServiceImpl"></bean>
</beans>
applicationContext-transaction.xml,可以配置数据库事务控制,aop,这里没有配置==
<?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:mvc="http://www.springframework.org/schema/mvc"xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 事务管理器
spring的jdbc控制类
-->
<!-- <bean id="" class=""></bean>--></beans>
其他:db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/yellowbike
jdbc.username=root
jdbc.password=1234
log4j.properties
### \u8BBE\u7F6E###
log4j.rootLogger = debug,stdout,D,E### \u8F93\u51FA\u4FE1\u606F\u5230\u63A7\u5236\u62AC ###
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n
四、总结
- 主要目的;
- springMVC与mybatis整合整体思路;
- 代码实例;
- 以后对事务控制和spring要进一步了解;
下载链接:点击打开链接
JavaWeb——springMVC、mybatis与spring的整合相关推荐
- SSM(Spring+SpringMVC+Mybatis)框架环境搭建(整合步骤)(一)
SSM(Spring+SpringMVC+Mybatis)框架环境搭建(整合步骤)(一) 1. 前言 最近在写毕设过程中,重新梳理了一遍SSM框架,特此记录一下. 附上源码:https://gitee ...
- Java框架搭建-Maven、Mybatis、Spring MVC整合搭建
Java框架搭建-Maven.Mybatis.Spring MVC整合搭建 1. 下载eclipse 到网站下载 http://www.eclipse.org/downloads/packages/e ...
- Spring、Mybatis、Spring MVC整合实例
Spring.Mybatis.Spring MVC整合实例笔记 源码地址:https://gitee.com/name168/SSM_Demo 1.Maven web项目创建(IDEA) 2.SSM整 ...
- 由“单独搭建Mybatis”到“Mybatis与Spring的整合/集成”
在J2EE领域,Hibernate与Mybatis是大家常用的持久层框架,它们各有特点,在持久层框架中处于领导地位. 本文主要介绍Mybatis(对于较小型的系统,特别是报表较多的系统,个人偏向Myb ...
- SSM Chapter 07 MyBatis与Spring的整合
SSM Chapter 07 MyBatis 与 Spring 的整合 笔记 本章目标: 掌握Spring与MyBatis的集成 掌握使用SqlSessionTemplate实现整合 掌握使用Mapp ...
- 【Spring+SpringMVC+Mybatis】利用SSM整合,完成用户登录、注册、修改密码系统
近年来,由于Struts2+Hibernate3+Spring3,这套SSH框架,Struts2屡次爆出安全漏洞,Hibernate就只会推行它HQL那套而越来越远离SQL查询关系数据库的本质,所以S ...
- 【Java】MyBatis与Spring框架整合(一)
本文将利用 Spring 对 MyBatis 进行整合,在对组件实现解耦的同时,还能使 MyBatis 框架的使用变得更加方便和简单. 整合思路 作为 Bean 容器,Spring 框架提供了 IoC ...
- 《JavaEE框架整合开发入门到实战——Spring+SpringMVC+MyBatis》读书笔记
加油生活,嗯,希望假期可以把这本书刷完,新年快乐,嘻嘻,今天是旧的一年里最后的一天,嗯,除夕一过,就25岁啦.希望新的一年里,学更多的东西,认识优秀的人,希望家人健康平安,希望自己少一些烦恼,总之先学 ...
- Spring+SpringMVC+Mybatis整合
一.简单测试工程搭建 1.Mybatis所需要的的jar包(包含数据库驱动包和相关的日志包).SpringMVC和Spring的jar包 2.然后构建一个基本的工程,这里我们使用mapper代理的方式 ...
最新文章
- AMD CPU真烂!售后服务也很可恶!
- 信息学奥赛一本通(C++)在线评测系统——基础(一)C++语言——1099:第n小的质数
- 机器学习:利用卷积神经网络实现图像风格迁移 (一)
- html 不透明阴影,CSS_css box-shadow阴影不透明的解决办法,如下面示例: 复制代码代码如 - phpStudy...
- word修改一处另一处自动修改_这么做让word自动记录修改明细,再也不用一个字一个字的核对了...
- linux mysql -uroot_linux mysql 怎么用
- Uniswap 24小时交易量9.7亿美元,占以太坊上Dex总量的54%
- Android开发笔记(八十)运行状态检查
- python全局代理_玩Python之HTTP代理
- 深度学习2.0-28.其他训练tricks-Early Stopping,dropout等
- Swift 团队开源 Collections,提供更多高效数据结构
- 判断一个数是否为质数
- laravel php artisan migrate 数据迁移时出现的[HY000][1045]错误
- BlackBerry 9900刷机
- 联想 Thinkserver TS250服务器RAID1 重建测试
- **汉服有哪些基本形制呢**
- linux 判断是否root权限,Android adb 判断是否有root权限
- cpu占用高 mongo_记一次 MongoDB 占用 CPU 过高问题的排查
- Fabric1.4.1多机部署
- android仿qq分组列表效果
热门文章
- java 格式化 浮点数_DecimalFormat的用法 Java 浮点数 Float Double 小数 格式化 保留小数位后几位等...
- 什么是事务ACID原则?(建议收藏)
- THREEJS - 动态标签(texture纹理方式)
- windows之Apache服务器搭建
- 中如何设置电气栅格_游戏中的设置界面如何设计?
- 得知发行组长老潘今天岗位上最后一天就要离开有感
- CSS 定位 (Positioning) 实例
- $.ajax()方法详解(转)
- php自定义session存储路径
- 【李宏毅2020 ML/DL】P20-21 Recurrent Neural Network | “Deep and structure is future.“