zoukankan      html  css  js  c++  java
  • Spring+SpringMVC+Mybatis整合

    1.2.1.导入相关jar包

    spring及springmvc的jar包

    aspectjweaver.jar
    spring-aop-4.3.25.RELEASE.jar
    spring-aspects-4.3.25.RELEASE.jar
    spring-beans-4.3.25.RELEASE.jar
    spring-context-4.3.25.RELEASE.jar
    spring-context-support-4.3.25.RELEASE.jar
    spring-core-4.3.25.RELEASE.jar
    spring-expression-4.3.25.RELEASE.jar
    spring-jdbc-4.3.25.RELEASE.jar
    spring-orm-4.3.25.RELEASE.jar
    spring-tx-4.3.25.RELEASE.jar
    spring-web-4.3.25.RELEASE.jar
    spring-webmvc-4.3.25.RELEASE.jar
    

    mybatis相关包

    mybatis-3.5.2.jar    //mybatis核心包
    mybatis-spring-1.3.3.jar  //  spring 和 mybatis的整合包
    

    日志包

    commons-logging-1.2.jar
    log4j-1.2.17.jar
    log4j-api-2.11.2.jar
    log4j-core-2.11.2.jar
    

    数据库包

    mysql-connector-java-5.1.47.jar
    

    分页插件包

    jsqlparser-2.1.jar
    pagehelper-5.1.10.jar
    

    连接池的包

    druid-1.1.20.jar
    

    jstl

    jstl-1.2.jar
    standard-1.1.2.jar
    

    1.2.2.配置文件

    数据库连接配置文件

    jdbc.properties

    jdbc.driver=com.mysql.jdbc.Driver
    jdbc.url=jdbc:mysql://127.0.0.1:3306/1009-mybatis?useUnicode=true&characterEncoding=utf8&useSSL=true
    jdbc.username=root
    jdbc.password=root
    #druid 内置监视器
    jdbc.filters=stat,log4j,wall
    # 最大连接数
    jdbc.maxActive=20
    # 最小连接数
    jdbc.minIdle=1
    

    日志的配置文件

    log4j.properties

    # Global logging configuration
    log4j.rootLogger=DEBUG, stdout
    # MyBatis logging configuration...
    log4j.logger.org.mybatis.example.BlogMapper=TRACE
    # Console output...
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
    
    

    spring配置文件

    1. spring-context
      <?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:aop="http://www.springframework.org/schema/aop"
      	xmlns:context="http://www.springframework.org/schema/context"
      	xmlns:mvc="http://www.springframework.org/schema/mvc"
      	xmlns:tx="http://www.springframework.org/schema/tx"
      	xsi:schemaLocation="http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
      		http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
      		http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd
      		http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd
      		http://www.springframework.org/schema/tx https://www.springframework.org/schema/tx/spring-tx.xsd">
      		
      		<!-- 引入其他配置文件 -->
      		<import resource="classpath:spring-dao.xml" />
      		<import resource="classpath:spring-tx.xml" />
      		<import resource="classpath:spring-service.xml" />
      		
      </beans>
      
    
    1. spring-dao.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:aop="http://www.springframework.org/schema/aop"
       	xmlns:context="http://www.springframework.org/schema/context"
       	xmlns:mvc="http://www.springframework.org/schema/mvc"
       	xmlns:tx="http://www.springframework.org/schema/tx"
       	xsi:schemaLocation="http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
       		http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
       		http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd
       		http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd
       		http://www.springframework.org/schema/tx https://www.springframework.org/schema/tx/spring-tx.xsd">
       		<!-- 配置数据源 -->
       		<!-- 1. 数据库配置文件 -->
       		<context:property-placeholder  location="classpath:jdbc.properties" system-properties-mode="FALLBACK"/>
       		<!-- 2.配置数据源 -->
       		<!-- 配置druid连接池 -->
       		<bean id="dataSource"  class="com.alibaba.druid.pool.DruidDataSource">
       			<property name="driverClassName" value="${jdbc.driver}"></property>
       			<property name="url" value="${jdbc.url}"></property>
       			<property name="username" value="${jdbc.username}"></property>
       			<property name="password" value="${jdbc.password}"></property>
       			<property name="filters" value="${jdbc.filters}"></property>
       			<property name="maxActive" value="${jdbc.maxActive}"></property>
       			<property name="minIdle" value="${jdbc.minIdle}"></property>
       		</bean>
       		<!-- SqlSession -->
       		<!-- 3.SqlSessionFactory -->
       		<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
       			<!-- 配置数据源 -->
       			<property name="dataSource" ref="dataSource"></property>
       			<!-- mybatis相关配置信息 -->
       			<!-- 配置mapper映射文件 -->
       			<property name="mapperLocations" value="classpath:mapper/*.xml"></property>
       			<!-- 配置别名 -->
       			<property name="typeAliasesPackage" value="com.sxt.pojo"></property>
       			<!-- 配置插件 -->
       			<property name="plugins">
       				<array>
       					<bean class="com.github.pagehelper.PageInterceptor"></bean>
       				</array>
       			</property>
       		</bean>
       		<!-- 4.使用SqlSessionFactory  生成mapper接口的代理类 -->
       		<!-- 使用mapper扫描 -->
       		<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
       			<property name="basePackage" value="com.sxt.mapper"></property>
       			<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
       		</bean>
       </beans>
       
    
    1. spring-tx.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:aop="http://www.springframework.org/schema/aop"
       	xmlns:context="http://www.springframework.org/schema/context"
       	xmlns:mvc="http://www.springframework.org/schema/mvc"
       	xmlns:tx="http://www.springframework.org/schema/tx"
       	xsi:schemaLocation="http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
       		http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
       		http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd
       		http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd
       		http://www.springframework.org/schema/tx https://www.springframework.org/schema/tx/spring-tx.xsd">
       		<!-- 事务管理器 -->
       		<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
       			<!-- 配置被管理的数据源 -->
       			<property name="dataSource" ref="dataSource"></property>
       		</bean>
       		<!-- 启用声明式事务 -->
       		<tx:annotation-driven transaction-manager="transactionManager"/>
       </beans>
       
    
    1. spring-service.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:aop="http://www.springframework.org/schema/aop"
       	xmlns:context="http://www.springframework.org/schema/context"
       	xmlns:mvc="http://www.springframework.org/schema/mvc"
       	xmlns:tx="http://www.springframework.org/schema/tx"
       	xsi:schemaLocation="http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
       		http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
       		http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd
       		http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd
       		http://www.springframework.org/schema/tx https://www.springframework.org/schema/tx/spring-tx.xsd">
       		<context:component-scan base-package="com.sxt.service.impl"></context:component-scan>
       </beans>
       
    

    springmvc配置文件

    <?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:aop="http://www.springframework.org/schema/aop"
    	xmlns:context="http://www.springframework.org/schema/context"
    	xmlns:mvc="http://www.springframework.org/schema/mvc"
    	xmlns:tx="http://www.springframework.org/schema/tx"
    	xsi:schemaLocation="http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
    		http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
    		http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd
    		http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd
    		http://www.springframework.org/schema/tx https://www.springframework.org/schema/tx/spring-tx.xsd">
    		<!-- 开启包扫描 -->
    		<context:component-scan base-package="com.sxt.controller"></context:component-scan>
    		<!-- 开启mvc注解 -->
    		<!-- 开启了注解式的映射器和适配器 -->
    		<mvc:annotation-driven />
    </beans>
    
    

    mybatis的mapper文件

    <?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">
      <!-- 
      	namespace : 命名空间
      	namespace作用: 
      		1.namespace + id 生成key,namespace 区分sql命令  sql命令的ID 不能重复
      		2.使用代理时 根据namespace 生成代理类
       -->
    <mapper namespace="com.sxt.mapper.UserMapper">
    	<!-- 添加 -->
    	<insert id="insert" parameterType="User" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
    		insert into  user value(0,#{name},#{password})
    	</insert>
    	<!-- 删除 -->
    	<delete id="delete" parameterType="int" >
    		delete from user where id = #{id}
    	</delete>
    	
    	<select id="selectOne" resultType="User" parameterType="int">
    		select id,name,password from user where id = #{id}
    	</select>
    	
    	<select id="selectList" resultType="User" >
    		select id,name,password from user
    	</select>
    	
    	<update id="update" parameterType="User">
    		update user 
    		set 
    			name = #{name},
    			password = #{password}
    		where 
    			id = #{id}	
    	</update>
    	
    </mapper>
    

    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>ssm01</display-name>
      <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
      </welcome-file-list>
      <!-- 配置listener   启动时 加载spring的配置文件 -->
      <listener>
      	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
      <context-param>
      	<param-name>contextConfigLocation</param-name>
      	<param-value>classpath:spring-context.xml</param-value>
      </context-param>
      
      
      <!-- 配置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:springmvc.xml</param-value>
      	</init-param>
      	<load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
      	<servlet-name>springmvc</servlet-name>
      	<url-pattern>*.do</url-pattern>
      </servlet-mapping>
      <!--  配置编码过滤器 -->
      <filter>
      	<filter-name>charsetFilter</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>
      </filter>
      <filter-mapping>
      	<filter-name>charsetFilter</filter-name>
      	<servlet-name>springmvc</servlet-name>
      </filter-mapping>
      <!-- 配置druid监控页面 -->
      <servlet>
      	<servlet-name>druid</servlet-name>
      	<servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class>
      </servlet>
      <servlet-mapping>
      	<servlet-name>druid</servlet-name>
      	<url-pattern>/druid/*</url-pattern>
      </servlet-mapping>
      
    </web-app>
    

    1.2.3.相关类

    pojo:

    package com.sxt.pojo;
    
    /**
     * 用户表实体类
     */
    public class User {
    	
    	private Integer id; //用户ID
    	private String  name; //用户名称
    	private String password; //用户密码
    	public Integer getId() {
    		return id;
    	}
    	public void setId(Integer id) {
    		this.id = id;
    	}
    	public String getName() {
    		return name;
    	}
    	public void setName(String name) {
    		this.name = name;
    	}
    	public String getPassword() {
    		return password;
    	}
    	public void setPassword(String password) {
    		this.password = password;
    	}
    	@Override
    	public String toString() {
    		return "User [id=" + id + ", name=" + name + ", password=" + password + "]";
    	}
    	
    }
    
    

    mapper

    package com.sxt.mapper;
    
    import java.util.List;
    
    import org.apache.ibatis.annotations.Param;
    
    import com.sxt.pojo.User;
    
    public interface UserMapper {
    	
    	
    	public int insert(User user);
    	
    	public int delete(@Param("id")Integer id);
    	
    	public User selectOne(@Param("id")Integer id);
    	
    	public List<User> selectList();
    	
    	public int update(User user);
    
    }
    
    

    service

    package com.sxt.service;
    
    import java.util.List;
    
    import com.github.pagehelper.Page;
    import com.sxt.pojo.User;
    
    public interface IUserService {
    	
    	public int insert(User user);
    	
    	public int delete(Integer id);
    	
    	public User selectOne(Integer id);
    	
    	public List<User> selectList();
    	
    	public Page<User> selectPage();
    	
    	public int update(User user);
    }
    
    
    package com.sxt.service.impl;
    
    import java.util.List;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import com.github.pagehelper.Page;
    import com.github.pagehelper.PageHelper;
    import com.sxt.mapper.UserMapper;
    import com.sxt.pojo.User;
    import com.sxt.service.IUserService;
    
    @Service
    public class UserServiceImpl implements IUserService {
    	
    	@Autowired
    	private UserMapper userMapper;
    	
    
    	@Override
    	public int insert(User user) {
    		return userMapper.insert(user);
    	}
    
    	@Override
    	public int delete(Integer id) {
    		return userMapper.delete(id);
    	}
    
    	@Override
    	public User selectOne(Integer id) {
    		return userMapper.selectOne(id);
    	}
    
    	@Override
    	public List<User> selectList() {
    		return userMapper.selectList();
    	}
    
    	@Override
    	public int update(User user) {
    		return userMapper.update(user);
    	}
    	
    	@Override
    	public Page<User> selectPage() {
    		Page<User> page = PageHelper.startPage(1, 5);
    		userMapper.selectList();
    		return page;
    	}
    	
    
    }
    
    
    

    controller

    package com.sxt.controller;
    
    import java.util.List;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    import com.sxt.pojo.User;
    import com.sxt.service.IUserService;
    
    @Controller
    @RequestMapping("user")
    public class UserController {
    	
    	@Autowired
    	private IUserService userService;
    	
    	@RequestMapping("list")
    	public String list(Model model) {
    		List<User> users = userService.selectList();
    		model.addAttribute("users", users);
    		return "/list.jsp";
    	}
    
    }
    
    
    

    jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
        <%@taglib uri="http://java.sun.com/jsp/jstl/core"  prefix="c"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    <table>
    	<tr>
    		<th>id</th>
    		<th>name</th>
    		<th>password</th>
    	</tr>
    	<c:forEach  items="${users}" var="user">
    		<tr>
    			<td>${user.id}</td>
    			<td>${user.name}</td>
    			<td>${user.password}</td>
    		</tr>
    	
    	</c:forEach>
    
    </table>
    </body>
    </html>
    
  • 相关阅读:
    Hybrid App(二)Cordova+android入门
    Hybrid App(一)App开发选型
    redis(一)Windows下安装redis服务、搭建redis主从复制
    玩转Nuget服务器搭建(三)
    玩转Nuget服务器搭建(二)
    玩转Nuget服务器搭建(一)
    Topshelf+Quartz.net+Dapper+Npoi(二)
    MySQL练习
    用过哪些SpringBoot注解
    Java 将数据写入全路径下的指定文件
  • 原文地址:https://www.cnblogs.com/lyang-a/p/12563313.html
Copyright © 2011-2022 走看看