文章详情

短信预约信息系统项目管理师 报名、考试、查分时间动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

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

2014-11-03 07:03

关注

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、新建项目

3、完善项目结构

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})
    

    
        select * from user where u_id = #{id}
    

    
        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

12、编写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
    
    
        dispatcherServlet
        class>org.springframework.web.servlet.DispatcherServletclass>
        
            contextConfigLocation
            classpath:ApplicationContext.xml
        
        1
    
    
        dispatcherServlet
        /
    

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

View Code

13、编写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

14、spring整合springmvc(ApplicationContext.xml引入springmvc.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">
    
    <import resource="springmvc.xml"/>
    
    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

15、编写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

16、创建必要的测试页面

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

17、整合完成,测试喽!

18、晒上项目结构图

19、其他配置文件

数据库配置文件:

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
阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-数据库
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯