SpringMvc4.0.0+Spring4.0.0+Mybatis3.2.7整合开发

环境: Eclipse mars.2 + jdk1.8 + SpringMvc4.0.0+Spring4.0.0+Mybatis3.2.7整合开发

导入jar包:

mysql数据库驱动包

c3p0包

Mybatis

  mybatis-3.2.7.jar

   lib/*.jar

Spring

  libs/*.jar(包含了springmvc的jar包)

Mybatis与Spring整合额外需要的包 mybatis-spring-1.2.2.jar

建好项目结构包

com.zns.blog.controller

com.zns.blog.service

com.zns.blog.mapper

com.zns.blog.domain

假设数据库有一张用户表 就2个字段 主键 和 账号

新建User类

package com.zns.blog.domain;


public class User {
    private int UserID;
    private String UserAccount;
    
    public int getUserID() {
        return UserID;
    }
    public void setUserID(int userID) {
        UserID = userID;
    }
    public String getUserAccount() {
        return UserAccount;
    }
    public void setUserAccount(String userAccount) {
        UserAccount = userAccount;
    }        
}

新建UserMapper接口

package com.zns.blog.mapper;

import com.zns.blog.domain.User;

public interface UserMapper {
    public User getByID(int id) throws Exception;
}

新建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.zns.blog.mapper.UserMapper">
    <select id="getByID" parameterType="int"
        resultType="com.zns.blog.domain.User">
        select * from User where UserID=#{UserID}
    </select>
</mapper>

src下增加jdbc.properties文件

jdbc.driver = com.mysql.jdbc.Driver

jdbc.url = jdbc:mysql://localhost:3306/mydb

jdbc.user = root

jdbc.password =123456

新建一个config资源文件夹  

然后在config下新增spring和mybatis文件夹

在mybatis下新建mybatis-config.xml

<?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>
  
  <!-- 由于使用mybatis与spring的整合包进行扫描 不需要配置mapper了
        前提是mapper.xml和mapper.java接口同名并在同一个目录
   -->
  
  <!-- 返回resultType为map时,如果数据为空的字段,则该字段会省略不显示,
   可以通过添加该配置,返回null -->
  <settings>
    <setting name="callSettersOnNulls" value="true"/>
  </settings>
  
</configuration>

在spring下新建applicationContext.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: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.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx.xsd">
    
    <!-- 配置连接池: -->
    <!-- 引入外部属性文件 -->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <!-- 配置C3P0连接池: -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.user}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
    
    <!-- spring和MyBatis整合,不需要mybatis的配置映射文件 -->  
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
        <property name="dataSource" ref="dataSource" />  
        <!-- 加载mybatis全局配置文件 -->  
        <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"></property>  
    </bean>  
    
    <!-- 配置mapper扫描器 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 扫描com.zns.blog.mapper这个包以及它的子包下的所有映射接口类,多个包逗号隔开 -->
        <property name="basePackage" value="com.zns.blog.mapper" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
    </bean>
    
    <!-- 配置Spring的事务管理器 与mabatis整合是用jdbc事务 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
    
     <!-- 拦截器方式配置事物 -->
    <!-- <tx:advice id="transactionAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="add*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="delete*" propagation="REQUIRED" />
            <tx:method name="get*" propagation="SUPPORTS" />
            <tx:method name="*" propagation="SUPPORTS" />
        </tx:attributes>
    </tx:advice>
    <aop:config>
        <aop:pointcut id="transactionPointcut" expression="execution(* com.zns.blog.service.*.*(..))" />
        <aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" />
    </aop:config> -->
    
    <!-- 配置servicce -->
    
    
</beans>

在spring下新建springmvc.xml

<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: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.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/mvc   
    http://www.springframework.org/schema/mvc/spring-mvc.xsd ">

    <!-- 扫描指定包 -->
    <context:component-scan base-package="com.zns.blog.controller"></context:component-scan>
        <context:component-scan base-package="com.zns.blog.service"></context:component-scan>    

    <!--注解映射器 -->
    <!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/> -->
    <!--注解适配器 -->
    <!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/> -->

    <!-- 使用这个可以代替上边的注解映射器和适配器 -->
    <mvc:annotation-driven></mvc:annotation-driven>

    <!-- 视图解析器 -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    </bean>
        
        <!-- 静态资源访问 -->  
    <mvc:resources mapping="/**" location="/" />
    
</beans>

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">

    <!-- springmvc前端控制器 -->
    <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:spring/springmvc.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>*.action</url-pattern>
    </servlet-mapping>
    
    <!-- springmvc前端控制器   restful风格 -->
    <servlet>
        <servlet-name>springmvc_rest</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>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc_rest</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    
    <!-- 配置Spring的核心监听器 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
    <filter>  
        <filter-name>characterEncodingFilter</filter-name>  
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
        <init-param>  
            <param-name>encoding</param-name>  
            <param-value>UTF-8</param-value>  
        </init-param>  
        <init-param>  
            <param-name>forceEncoding</param-name>  
            <param-value>true</param-value>  
        </init-param>  
    </filter>  
    <filter-mapping>  
        <filter-name>characterEncodingFilter</filter-name>  
        <url-pattern>/*</url-pattern>  
    </filter-mapping>

</web-app>

新建UserService

package com.zns.blog.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zns.blog.domain.User;
import com.zns.blog.mapper.UserMapper;

@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;
    public User getByID(int id) throws Exception{
          return userMapper.getByID(id);
    }
}

新建一个UserController

package com.zns.blog.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.zns.blog.domain.User;
import com.zns.blog.service.UserService;

@Controller
@RequestMapping("User")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("Detail")
    public ModelAndView Detail() throws Exception {
        User user = userService.getByID(1);
        System.out.println(user);
        ModelAndView modelAndView = new ModelAndView();
        //相当于加入到request域
        modelAndView.addObject("userInfo", user);
        modelAndView.setViewName("/xxx.jsp");
        return modelAndView;
    }
}

启动测试访问/项目名/User/Detail 可以看到打印的用户信息  基本整合完毕

猜你喜欢

转载自www.cnblogs.com/zengnansheng/p/10385999.html