JavaWeb——springMVC、mybatis与spring的整合
来源:互联网 发布:淘宝申请售后次数 编辑:程序博客网 时间:2024/04/29 08:46
一、引言
距离上次写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 mapperPUBLIC "-//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>
package com.xcy.service;import com.xcy.po.User;public interface UserService {public User findUser (int i) throws Exception;}
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;@Controllerpublic class UserController {@Autowiredprivate 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>
<?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>
<?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.Driverjdbc.url=jdbc:mysql://localhost:3306/yellowbikejdbc.username=rootjdbc.password=1234
### \u8BBE\u7F6E###log4j.rootLogger = debug,stdout,D,E### \u8F93\u51FA\u4FE1\u606F\u5230\u63A7\u5236\u62AC ###log4j.appender.stdout = org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.Target = System.outlog4j.appender.stdout.layout = org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n
四、总结
- 主要目的;
- springMVC与mybatis整合整体思路;
- 代码实例;
- 以后对事务控制和spring要进一步了解;
下载链接:点击打开链接
阅读全文
1 0
- JavaWeb——springMVC、mybatis与spring的整合
- javaweb—SpringMVC+Spring+MyBatis整合
- SpringMVC,Spring与Mybatis框架整合——ssm模板
- mybatis与spring、springmvc整合
- MyBatis与Spring、SpringMVC整合
- Spring+SpringMVC+MyBatis的整合
- JAVAWEB开发之SpringMVC详解(一)——SpringMVC的框架原理、架构简介、与mybatis整合和注解方式的使用、
- JAVAWEB开发之SpringMVC详解(一)——SpringMVC的框架原理、架构简介、与mybatis整合和注解方式的使用、
- idea 部署spring springMVC 与mybatis的整合
- springmvc与mybatis的整合
- SpringMVC与MyBatis的整合
- Mybaties入门 Mybatis与SpringMVC,spring整合
- SSM:Spring,SpringMVC与MyBatis整合
- SpringMVC学习(四)——Spring、MyBatis和SpringMVC的整合
- Spring-SpringMVC-Mybatis整合
- springmvc+spring+mybatis整合
- springmvc+spring+mybatis整合
- Spring+SpringMVC+Mybatis整合
- insmod过程详解
- 源码安装mysql5.6(Centos6)
- python数据分析应用之机器学习库(占坑)
- Java并发修改错误ConcurrentModificationException分析
- 干掉bug的通用套路
- JavaWeb——springMVC、mybatis与spring的整合
- 【缓存技术原理】浏览器端缓存机制详解
- wex5 实战 二维码生成,扫描,蓝牙打印
- Jetson TX1串口调试
- Android studio 无法连接识别检测各种模拟器和手机的问题。
- 银行卡号扫描识别OCR技术配置要求
- spring cloud 【开篇】
- codeforces 401D Roman and Numbers (数位dp)
- Android NDK之旅-OpenGL 初探