SSM(Spring+SpringMVC+Mybatis)框架整合

1、数据准备

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for `admin`
-- ----------------------------
DROP TABLE IF EXISTS `admin`;
CREATE TABLE `admin` (
  `a_id` int(11) NOT NULL AUTO_INCREMENT,
  `a_name` varchar(20) NOT NULL,
  `a_pwd` varchar(20) NOT NULL,
  PRIMARY KEY (`a_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of admin
-- ----------------------------

-- ----------------------------
-- Table structure for `book`
-- ----------------------------
DROP TABLE IF EXISTS `book`;
CREATE TABLE `book` (
  `b_id` int(11) NOT NULL AUTO_INCREMENT,
  `b_ISBN` varchar(20) NOT NULL,
  `b_name` varchar(40) NOT NULL,
  `b_author` varchar(20) NOT NULL,
  `b_cid` int(11) NOT NULL,
  `b_cover` varchar(50) NOT NULL,
  `b_publish_time` date NOT NULL,
  `b_remark` varchar(255) NOT NULL,
  `b_num` int(11) NOT NULL,
  PRIMARY KEY (`b_id`),
  KEY `b_cid` (`b_cid`),
  CONSTRAINT `book_ibfk_1` FOREIGN KEY (`b_cid`) REFERENCES `category` (`c_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of book
-- ----------------------------

-- ----------------------------
-- Table structure for `category`
-- ----------------------------
DROP TABLE IF EXISTS `category`;
CREATE TABLE `category` (
  `c_id` int(11) NOT NULL AUTO_INCREMENT,
  `c_name` varchar(20) NOT NULL,
  PRIMARY KEY (`c_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of category
-- ----------------------------

-- ----------------------------
-- Table structure for `log`
-- ----------------------------
DROP TABLE IF EXISTS `log`;
CREATE TABLE `log` (
  `l_id` int(11) NOT NULL AUTO_INCREMENT,
  `l_uid` int(11) NOT NULL,
  `l_bid` int(11) NOT NULL,
  `l_begintime` date NOT NULL,
  `l_endtime` date NOT NULL,
  PRIMARY KEY (`l_id`),
  KEY `l_uid` (`l_uid`),
  KEY `l_bid` (`l_bid`),
  CONSTRAINT `log_ibfk_1` FOREIGN KEY (`l_uid`) REFERENCES `user` (`u_id`),
  CONSTRAINT `log_ibfk_2` FOREIGN KEY (`l_bid`) REFERENCES `book` (`b_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of log
-- ----------------------------

-- ----------------------------
-- Table structure for `user`
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
  `u_id` int(11) NOT NULL AUTO_INCREMENT,
  `u_name` varchar(20) NOT NULL,
  `u_pwd` varchar(20) NOT NULL,
  PRIMARY KEY (`u_id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES ('1', '鲁班', '1234567890');
INSERT INTO `user` VALUES ('2', '杜甫', '123456');
View Code

2、新建项目

SSM(Spring+SpringMVC+Mybatis)框架整合_第1张图片

SSM(Spring+SpringMVC+Mybatis)框架整合_第2张图片

SSM(Spring+SpringMVC+Mybatis)框架整合_第3张图片

SSM(Spring+SpringMVC+Mybatis)框架整合_第4张图片

3、完善项目结构

SSM(Spring+SpringMVC+Mybatis)框架整合_第5张图片

SSM(Spring+SpringMVC+Mybatis)框架整合_第6张图片

4、导入所需jar包


    UTF-8
    1.8
    1.8



    
    
        org.springframework
        spring-webmvc
        4.3.13.RELEASE
    
    
        org.springframework
        spring-test
        4.3.13.RELEASE
    
    
        org.springframework
        spring-jdbc
        4.3.13.RELEASE
    
    
    
        org.mybatis
        mybatis
        3.4.5
    
    
        org.mybatis
        mybatis-spring
        1.3.1
    
    
    
        junit
        junit
        4.12
    
    
    
        log4j
        log4j
        1.2.17
    
    
    
        mysql
        mysql-connector-java
        5.1.43
    
    
    
        c3p0
        c3p0
        0.9.1.2
    
    
    
        javax.servlet
        jsp-api
        2.0
    
    
    
        javax.servlet.jsp.jstl
        jstl-api
        1.2
    
    
    
        javax.servlet
        servlet-api
        3.0
    
    
    
        org.projectlombok
        lombok
        1.16.18
    
View Code

5、编写实体类

User:

package com.ssm.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User implements Serializable {
    private Integer id;
    private String name;
    private String pwd;
}
View Code

 

6、编写dao层

UserDao:

package com.ssm.dao;
import com.ssm.pojo.User;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository("userDao")
public interface UserDao {
    void addUser(User user);

    void deleteUser(Integer id);

    void updateInfo(User user);

    List queryAll();

    User queryById(Integer id);
}
View Code

 

7、根据dao层编写xml映射配置文件

UserMapper.xml:


DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">


    
        
        
        
    


    
          insert into user(u_name,u_pwd) values (#{name},#{pwd})
    

    

    

    
        update user set u_name = #{name},u_pwd = #{pwd} where u_id = #{id}
    

    
        delete from user where u_id = #{id}
    
View Code

8、编写Mybatis主配置文件(mybatis-config.xml)


DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">


    
        <package name="com.ssm.pojo"/>
    
    
        
    

View Code

9、编写Spring全局配置文件(ApplicationContext.xml)



       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       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">

    
    package="com.ssm">
        
    
    
    
    
    class="com.mchange.v2.c3p0.ComboPooledDataSource">
        
        
        
        
    
    
    class="org.mybatis.spring.SqlSessionFactoryBean">
        
        
    
    
    class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        
        
    
    
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        
    
View Code

10、编写service层

IUserService:

package com.ssm.service;

import com.ssm.pojo.User;

import java.util.List;

public interface IUserService {
    void addUser(User user);

    void deleteUser(Integer id);

    void updateInfo(User user);

    List queryAll();

    User queryById(Integer id);
}
View Code

UserService:

package com.ssm.service.impl;
import com.ssm.dao.UserDao;
import com.ssm.pojo.User;
import com.ssm.service.IUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("userService")
public class UserService implements IUserService {
    @Resource
    private UserDao userDao;

    @Override
    public void addUser(User user) {
        userDao.addUser(user);
    }

    @Override
    public void deleteUser(Integer id) {
        userDao.deleteUser(id);
    }

    @Override
    public void updateInfo(User user) {
        userDao.updateInfo(user);
    }

    @Override
    public List queryAll() {
        return userDao.queryAll();
    }

    @Override
    public User queryById(Integer id) {
        return userDao.queryById(id);
    }
}
View Code

11、spring整合Mybatis完成(测试)

import com.ssm.pojo.User;
import com.ssm.service.IUserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;
import java.util.List;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:ApplicationContext.xml")
public class UserTest {
    @Resource
    IUserService userService;

    @Test
    public void getAll() {
        List users = userService.queryAll();
        for (User user : users) {
            System.out.println(user);
        }
    }
}
View Code

SSM(Spring+SpringMVC+Mybatis)框架整合_第7张图片

12、编写springmvc配置文件(springmvc.xml)



       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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/mvc
    https://www.springframework.org/schema/mvc/spring-mvc.xsd">
    
    default-servlet-handler/>
    
    
    
    package="com.ssm.controller"/>
    
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        
        
    

View Code

13、编写web.xml



         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

    Archetype Created Web Application
    
    
        class>org.springframework.web.context.ContextLoaderListenerclass>
    
    
    
        contextConfigLocation
        classpath:ApplicationContext.xml
    
    
    
        dispatcherServlet
        class>org.springframework.web.servlet.DispatcherServletclass>
        
            contextConfigLocation
            classpath:springmvc.xml
        
        1
    
    
        dispatcherServlet
        /
    

    
    
        characterEncodingFilter
        class>org.springframework.web.filter.CharacterEncodingFilterclass>
    
    
        characterEncodingFilter
        /*
    

View Code

14、编写controller层

UserController:

package com.ssm.controller;

import com.ssm.pojo.User;
import com.ssm.service.IUserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;

@RequestMapping("/user")
@Controller
public class UserController {
    @Resource
    IUserService userService;

    @RequestMapping("getUserInfo")
    public String test(Model model) {
        User user = userService.queryById(1);
        model.addAttribute("user", user);
        return "success";
    }
}
View Code

15、创建必要的测试页面

index.jsp:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Title


点我测试!

View Code

success.jsp:

<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>


    Title


Name:${user.name}
PassWord:${user.pwd}

View Code

16、整合完成,测试喽!

SSM(Spring+SpringMVC+Mybatis)框架整合_第8张图片

SSM(Spring+SpringMVC+Mybatis)框架整合_第9张图片

17、晒上项目结构图

SSM(Spring+SpringMVC+Mybatis)框架整合_第10张图片

SSM(Spring+SpringMVC+Mybatis)框架整合_第11张图片

18、其他配置文件

数据库配置文件:

jdbc.driver=com.mysql.jdbc.Driver
#数据库地址
jdbc.url=jdbc:mysql://localhost:3306/booksys?useUnicode=true&characterEncoding=utf8
#用户名
jdbc.username=root
#密码
jdbc.password=123456
#最大连接数
c3p0.maxPoolSize=30
#最小连接数
c3p0.minPoolSize=10
#关闭连接后不自动commit
c3p0.autoCommitOnClose=false
#获取连接超时时间
c3p0.checkoutTimeout=10000
#当获取连接失败重试次数
c3p0.acquireRetryAttempts=2
View Code

log4j日志配置文档:

log4j.rootLogger=debug,stdout, R
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=example.log
log4j.appender.R.MaxFileSize=100KB
# Keep one backup file
log4j.appender.R.MaxBackupIndex=5
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n
View Code

你可能感兴趣的:(SSM(Spring+SpringMVC+Mybatis)框架整合)