当前位置: 首页 > news >正文

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 可以看到打印的用户信息  基本整合完毕

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

相关文章:

  • js-时间戳转字符串
  • PYTHON2.day02
  • POJ3635 Full Tank
  • 家庭记事本开发进度1
  • Winodws 10 美化与调优
  • matlab-基础 快捷键 命令行窗口 输入多行命令
  • Redis在Web项目中的应用与实践
  • TLS 1.3 Handshake Protocol (下)
  • 前端_面试
  • 67 亿美金搞个图,创建知识图谱的成本有多高你知道吗?
  • 重学前端-css选择器
  • 对象引论
  • Windows Core OS预计将更多地依赖于这些组件
  • 【技术性】Search知识
  • 什么是Javascript函数节流?
  • 08.Android之View事件问题
  • 77. Combinations
  • Angular6错误 Service: No provider for Renderer2
  • java概述
  • laravel 用artisan创建自己的模板
  • MaxCompute访问TableStore(OTS) 数据
  • Python 使用 Tornado 框架实现 WebHook 自动部署 Git 项目
  • windows下mongoDB的环境配置
  • Xmanager 远程桌面 CentOS 7
  • 计算机常识 - 收藏集 - 掘金
  • 如何实现 font-size 的响应式
  • 学习Vue.js的五个小例子
  • 大数据全解:定义、价值及挑战
  • 没有任何编程基础可以直接学习python语言吗?学会后能够做什么? ...
  • ​比特币大跌的 2 个原因
  • ​业务双活的数据切换思路设计(下)
  • (1) caustics\
  • (12)Linux 常见的三种进程状态
  • (四)JPA - JQPL 实现增删改查
  • (四)搭建容器云管理平台笔记—安装ETCD(不使用证书)
  • (小白学Java)Java简介和基本配置
  • (一)Linux+Windows下安装ffmpeg
  • ***监测系统的构建(chkrootkit )
  • .bat批处理(十):从路径字符串中截取盘符、文件名、后缀名等信息
  • .net 4.0 A potentially dangerous Request.Form value was detected from the client 的解决方案
  • .Net 8.0 新的变化
  • .NET CORE 2.0发布后没有 VIEWS视图页面文件
  • .NET CORE Aws S3 使用
  • .NET Standard、.NET Framework 、.NET Core三者的关系与区别?
  • .Net 路由处理厉害了
  • .net对接阿里云CSB服务
  • @media screen 针对不同移动设备
  • [2018-01-08] Python强化周的第一天
  • [Android]使用Android打包Unity工程
  • [C++]unordered系列关联式容器
  • [fsevents@^2.1.2] optional install error: Package require os(darwin) not compatible with your platfo
  • [IE编程] WebBrowser控件中设置页面的缩放
  • [IE编程] 如何获得IE版本号
  • [Linux] Apache的配置与运用
  • [Linux] 一文理解HTTPS协议:什么是HTTPS协议、HTTPS协议如何加密数据、什么是CA证书(数字证书)...