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>


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;@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>


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.Driverjdbc.url=jdbc:mysql://localhost:3306/yellowbikejdbc.username=rootjdbc.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.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
原创粉丝点击