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>
  
  
  
  
  <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"/>
    
    <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>
    
      
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
        <property name="dataSource" ref="dataSource" />  
          
        <property name="configLocation" value="classpath:mybatis/mybatis-config.xml">property>  
    bean>  
    
    
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        
        <property name="basePackage" value="com.zns.blog.mapper" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
    bean>
    
    
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    bean>
    
     
    
    
    
    
    
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>    

    
    
    
    

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

    
    <servlet>
        <servlet-name>springmvcservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        <init-param>
            <param-name>contextConfigLocationparam-name>
            <param-value>classpath:spring/springmvc.xmlparam-value>
        init-param>
    servlet>
    <servlet-mapping>
        <servlet-name>springmvcservlet-name>
        <url-pattern>*.actionurl-pattern>
    servlet-mapping>
    
    
    <servlet>
        <servlet-name>springmvc_restservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        <init-param>
            <param-name>contextConfigLocationparam-name>
            <param-value>classpath:spring/springmvc.xmlparam-value>
        init-param>
    servlet>
    <servlet-mapping>
        <servlet-name>springmvc_restservlet-name>
        <url-pattern>/url-pattern>
    servlet-mapping>
    
    
    <context-param>
        <param-name>contextConfigLocationparam-name>
        <param-value>classpath:spring/applicationContext.xmlparam-value>
    context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
    listener>
    
    <filter>  
        <filter-name>characterEncodingFilterfilter-name>  
        <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>  
        <init-param>  
            <param-name>encodingparam-name>  
            <param-value>UTF-8param-value>  
        init-param>  
        <init-param>  
            <param-name>forceEncodingparam-name>  
            <param-value>trueparam-value>  
        init-param>  
    filter>  
    <filter-mapping>  
        <filter-name>characterEncodingFilterfilter-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 可以看到打印的用户信息  基本整合完毕

转载于:https://www.cnblogs.com/zengnansheng/p/10385999.html

你可能感兴趣的:(SpringMvc4.0.0+Spring4.0.0+Mybatis3.2.7整合开发)