Awesome Open Source
Awesome Open Source

项目基本情况

  1. 简单的SMM框架使用
  2. 从0到1慢慢丰富,不定时更新新的内容到项目中
  3. 如果有什么问题,欢迎指正或者交流
  4. 有好的学习资料欢迎共享
  5. 本项目随便使用,没有任何限制

SSM_BookSystem SSM框架基础

SSM_BookSystem V0 - 版本详细说明

说明:本项目目前包含基础的CRUD

作者:hisenyuan

主页:hisen.me

博客:http://www.cnblogs.com/hisen/

预览:


搭建过程:

一般idea创建工程的过程

打开idea ---> File ---> new ---> project ---> maven --->

create from archetype ---> maven-archetype-webapp ---> 接下来一般默认即可

最后在main目录下新建java文件夹,并且mark as source root。

首先创建如下的目录结构即可:

├── java
│   └── com
│       └── hisen
│           ├── dao
│           │   └── BookDao.java
│           ├── entity
│           │   └── Book.java
│           ├── service
│           │   ├── BookService.java
│           │   └── impl
│           │       └── BookServiceImpl.java
│           └── web
│               └── BookController.java
├── resources
│   ├── jdbc.properties
│   ├── logback.xml
│   ├── mapper
│   │   └── BookMapper.xml
│   ├── mybatis-config.xml
│   └── spring
│       ├── spring-dao.xml
│       ├── spring-service.xml
│       └── spring-web.xml
└── webapp
    ├── index.jsp
    └── WEB-INF
        ├── jsp
        │   ├── detail.jsp
        │   └── list.jsp
        └── web.xml

第一步:添加Spring、Spring MVC、Mybatis的依赖

都配有详细的说明,这里不再重复

详情请看pom.xml

第二步:添加数据库配置文件

├── resources
│   ├── jdbc.properties

创建数据库配置文件,内容如下

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/booksystem?useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=hisen

这里需要创建一个数据库,名为:booksystem

建表语句如下:

CREATE TABLE `book` (
  `book_id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '图书ID',
  `name` varchar(100) NOT NULL COMMENT '图书名称',
  `number` int(11) NOT NULL COMMENT '图书数量',
  `detail` varchar(200) NOT NULL COMMENT '图书描述',
  PRIMARY KEY (`book_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='图书表'

表结构如下:

+---------+--------------+------+-----+---------+----------------+
| Field   | Type         | Null | Key | Default | Extra          |
+---------+--------------+------+-----+---------+----------------+
| book_id | bigint(20)   | NO   | PRI | NULL    | auto_increment |
| name    | varchar(100) | NO   |     | NULL    |                |
| number  | int(11)      | NO   |     | NULL    |                |
| detail  | varchar(200) | NO   |     | NULL    |                |
+---------+--------------+------+-----+---------+----------------+

第三步:添加mybatis配置文件

├── resources
│   ├── mybatis-config.xml

在resources目录下新建文件: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>
    <!-- 使用jdbc的getGeneratedKeys获取数据库自增主键值 -->
    <setting name="useGeneratedKeys" value="true" />
    <!-- 使用列别名替换列名 默认:true -->
    <setting name="useColumnLabel" value="true" />
    <!-- 开启驼峰命名转换:Table{create_time} -> Entity{createTime} -->
    <setting name="mapUnderscoreToCamelCase" value="true" />
  </settings>
</configuration>

第四步:添加Spring配置文件

在resources/spring目录下新建三个文件:

│   └── spring
│       ├── spring-dao.xml
│       ├── spring-service.xml
│       └── spring-web.xml

详细内容详见:resources/spring/

第五步:添加logback配置文件

logback配置比log4j要简单点,功能类似

├── resources
│   ├── logback.xml

在resources文件夹下新建文件:logback.xml

<?xml version="1.0" encoding="UTF-8" ?>
<configuration debug="true">
  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
      <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
  </appender>
  <!--开启debug日志模式,在控制台打印日志-->
  <root level="debug">
    <appender-ref ref="STDOUT" />
  </root>
</configuration>

第六步:创建DAO、entity

如下两个文件:BookDao.java、Book.java

│   └── com
│       └── hisen
│           ├── dao
│           │   └── BookDao.java
│           ├── entity
│           │   └── Book.java

详细内容:BookDao.java

详细内容:Book.java

第七步:创建mybatis mapper文件

在resources/mapper/目录下创建:resources/mapper/BookMapper.xml

在mapper文件中需要用到第六步的内容

创建测试类:

  1. 测试基类:查看代码BaseTest.java
  2. BookDao:查看代码BookDaoTest.java
  3. 测试:addBook (由于没有预先准备数据,所以就先添加)
20:12:26.674 [main] DEBUG o.m.s.t.SpringManagedTransaction - JDBC Connection [[email protected]] will not be managed by Spring
20:12:26.709 [main] DEBUG com.hisen.dao.BookDao.addBook - ==>  Preparing: INSERT INTO book(`book_id`, `name`, `number`,`detail`) VALUES(?, ?, ?, ?) 
20:12:26.777 [main] DEBUG com.hisen.dao.BookDao.addBook - ==> Parameters: 0(Long), 活着0(String), 100(Integer), 描述0(String)
20:12:26.783 [main] DEBUG com.hisen.dao.BookDao.addBook - <==    Updates: 1
20:12:26.791 [main] DEBUG org.mybatis.spring.SqlSessionUtils - Closing non transactional SqlSession [[email protected]]
20:12:26.792 [main] DEBUG org.mybatis.spring.SqlSessionUtils - Creating a new SqlSession
20:12:26.793 [main] DEBUG org.mybatis.spring.SqlSessionUtils - SqlSession [[email protected]] was not registered for synchronization because synchronization is not active
20:12:26.793 [main] DEBUG o.m.s.t.SpringManagedTransaction - JDBC Connection [[email protected]] will not be managed by Spring
20:12:26.793 [main] DEBUG com.hisen.dao.BookDao.addBook - ==>  Preparing: INSERT INTO book(`book_id`, `name`, `number`,`detail`) VALUES(?, ?, ?, ?) 
20:12:26.794 [main] DEBUG com.hisen.dao.BookDao.addBook - ==> Parameters: 0(Long), 活着1(String), 101(Integer), 描述1(String)
20:12:26.798 [main] DEBUG com.hisen.dao.BookDao.addBook - <==    Updates: 1
20:12:26.798 [main] DEBUG org.mybatis.spring.SqlSessionUtils - Closing non transactional SqlSession [[email protected]]
20:12:26.798 [main] DEBUG org.mybatis.spring.SqlSessionUtils - Creating a new SqlSession
20:12:26.799 [main] DEBUG org.mybatis.spring.SqlSessionUtils - SqlSession [[email protected]] was not registered for synchronization because synchronization is not active
20:12:26.799 [main] DEBUG o.m.s.t.SpringManagedTransaction - JDBC Connection [[email protected]] will not be managed by Spring
20:12:26.799 [main] DEBUG com.hisen.dao.BookDao.addBook - ==>  Preparing: INSERT INTO book(`book_id`, `name`, `number`,`detail`) VALUES(?, ?, ?, ?) 
20:12:26.799 [main] DEBUG com.hisen.dao.BookDao.addBook - ==> Parameters: 0(Long), 活着2(String), 102(Integer), 描述2(String)
20:12:26.804 [main] DEBUG com.hisen.dao.BookDao.addBook - <==    Updates: 1
20:12:26.804 [main] DEBUG org.mybatis.spring.SqlSessionUtils - Closing non transactional SqlSession [[email protected]]
20:12:26.805 [main] DEBUG org.mybatis.spring.SqlSessionUtils - Creating a new SqlSession
20:12:26.805 [main] DEBUG org.mybatis.spring.SqlSessionUtils - SqlSession [[email protected]] was not registered for synchronization because synchronization is not active
20:12:26.805 [main] DEBUG o.m.s.t.SpringManagedTransaction - JDBC Connection [[email protected]] will not be managed by Spring
20:12:26.805 [main] DEBUG com.hisen.dao.BookDao.addBook - ==>  Preparing: INSERT INTO book(`book_id`, `name`, `number`,`detail`) VALUES(?, ?, ?, ?) 
20:12:26.805 [main] DEBUG com.hisen.dao.BookDao.addBook - ==> Parameters: 0(Long), 活着3(String), 103(Integer), 描述3(String)
20:12:26.810 [main] DEBUG com.hisen.dao.BookDao.addBook - <==    Updates: 1
20:12:26.811 [main] DEBUG org.mybatis.spring.SqlSessionUtils - Closing non transactional SqlSession [[email protected]]
20:12:26.811 [main] DEBUG org.mybatis.spring.SqlSessionUtils - Creating a new SqlSession
20:12:26.811 [main] DEBUG org.mybatis.spring.SqlSessionUtils - SqlSession [[email protected]] was not registered for synchronization because synchronization is not active
20:12:26.811 [main] DEBUG o.m.s.t.SpringManagedTransaction - JDBC Connection [[email protected]] will not be managed by Spring
20:12:26.811 [main] DEBUG com.hisen.dao.BookDao.addBook - ==>  Preparing: INSERT INTO book(`book_id`, `name`, `number`,`detail`) VALUES(?, ?, ?, ?) 
20:12:26.812 [main] DEBUG com.hisen.dao.BookDao.addBook - ==> Parameters: 0(Long), 活着4(String), 104(Integer), 描述4(String)
20:12:26.816 [main] DEBUG com.hisen.dao.BookDao.addBook - <==    Updates: 1
20:12:26.817 [main] DEBUG org.mybatis.spring.SqlSessionUtils - Closing non transactional SqlSession [[email protected]]
20:12:26.817 [main] DEBUG org.mybatis.spring.SqlSessionUtils - Creating a new SqlSession
20:12:26.817 [main] DEBUG org.mybatis.spring.SqlSessionUtils - SqlSession [[email protected]] was not registered for synchronization because synchronization is not active
20:12:26.817 [main] DEBUG o.m.s.t.SpringManagedTransaction - JDBC Connection [[email protected]] will not be managed by Spring
20:12:26.817 [main] DEBUG com.hisen.dao.BookDao.addBook - ==>  Preparing: INSERT INTO book(`book_id`, `name`, `number`,`detail`) VALUES(?, ?, ?, ?) 
20:12:26.818 [main] DEBUG com.hisen.dao.BookDao.addBook - ==> Parameters: 0(Long), 活着5(String), 105(Integer), 描述5(String)
20:12:26.822 [main] DEBUG com.hisen.dao.BookDao.addBook - <==    Updates: 1
20:12:26.823 [main] DEBUG org.mybatis.spring.SqlSessionUtils - Closing non transactional SqlSession [[email protected]]
20:12:26.823 [main] DEBUG org.mybatis.spring.SqlSessionUtils - Creating a new SqlSession
20:12:26.823 [main] DEBUG org.mybatis.spring.SqlSessionUtils - SqlSession [[email protected]] was not registered for synchronization because synchronization is not active
20:12:26.824 [main] DEBUG o.m.s.t.SpringManagedTransaction - JDBC Connection [[email protected]] will not be managed by Spring
20:12:26.825 [main] DEBUG com.hisen.dao.BookDao.addBook - ==>  Preparing: INSERT INTO book(`book_id`, `name`, `number`,`detail`) VALUES(?, ?, ?, ?) 
20:12:26.825 [main] DEBUG com.hisen.dao.BookDao.addBook - ==> Parameters: 0(Long), 活着6(String), 106(Integer), 描述6(String)
20:12:26.830 [main] DEBUG com.hisen.dao.BookDao.addBook - <==    Updates: 1
20:12:26.830 [main] DEBUG org.mybatis.spring.SqlSessionUtils - Closing non transactional SqlSession [[email protected]]
20:12:26.830 [main] DEBUG org.mybatis.spring.SqlSessionUtils - Creating a new SqlSession
20:12:26.830 [main] DEBUG org.mybatis.spring.SqlSessionUtils - SqlSession [[email protected]] was not registered for synchronization because synchronization is not active
20:12:26.831 [main] DEBUG o.m.s.t.SpringManagedTransaction - JDBC Connection [[email protected]] will not be managed by Spring
20:12:26.831 [main] DEBUG com.hisen.dao.BookDao.addBook - ==>  Preparing: INSERT INTO book(`book_id`, `name`, `number`,`detail`) VALUES(?, ?, ?, ?) 
20:12:26.832 [main] DEBUG com.hisen.dao.BookDao.addBook - ==> Parameters: 0(Long), 活着7(String), 107(Integer), 描述7(String)
20:12:26.836 [main] DEBUG com.hisen.dao.BookDao.addBook - <==    Updates: 1
20:12:26.836 [main] DEBUG org.mybatis.spring.SqlSessionUtils - Closing non transactional SqlSession [[email protected]]
20:12:26.836 [main] DEBUG org.mybatis.spring.SqlSessionUtils - Creating a new SqlSession
20:12:26.836 [main] DEBUG org.mybatis.spring.SqlSessionUtils - SqlSession [[email protected]] was not registered for synchronization because synchronization is not active
20:12:26.836 [main] DEBUG o.m.s.t.SpringManagedTransaction - JDBC Connection [[email protected]] will not be managed by Spring
20:12:26.836 [main] DEBUG com.hisen.dao.BookDao.addBook - ==>  Preparing: INSERT INTO book(`book_id`, `name`, `number`,`detail`) VALUES(?, ?, ?, ?) 
20:12:26.837 [main] DEBUG com.hisen.dao.BookDao.addBook - ==> Parameters: 0(Long), 活着8(String), 108(Integer), 描述8(String)
20:12:26.842 [main] DEBUG com.hisen.dao.BookDao.addBook - <==    Updates: 1
20:12:26.842 [main] DEBUG org.mybatis.spring.SqlSessionUtils - Closing non transactional SqlSession [[email protected]]
20:12:26.843 [main] DEBUG org.mybatis.spring.SqlSessionUtils - Creating a new SqlSession
20:12:26.843 [main] DEBUG org.mybatis.spring.SqlSessionUtils - SqlSession [[email protected]] was not registered for synchronization because synchronization is not active
20:12:26.843 [main] DEBUG o.m.s.t.SpringManagedTransaction - JDBC Connection [[email protected]] will not be managed by Spring
20:12:26.843 [main] DEBUG com.hisen.dao.BookDao.addBook - ==>  Preparing: INSERT INTO book(`book_id`, `name`, `number`,`detail`) VALUES(?, ?, ?, ?) 
20:12:26.843 [main] DEBUG com.hisen.dao.BookDao.addBook - ==> Parameters: 0(Long), 活着9(String), 109(Integer), 描述9(String)
20:12:26.848 [main] DEBUG com.hisen.dao.BookDao.addBook - <==    Updates: 1
20:12:26.848 [main] DEBUG org.mybatis.spring.SqlSessionUtils - Closing non transactional SqlSession [[email protected]]
  1. 测试:queryById
20:15:03.947 [main] DEBUG o.m.s.t.SpringManagedTransaction - JDBC Connection [[email protected]] will not be managed by Spring
20:15:03.972 [main] DEBUG com.hisen.dao.BookDao.queryById - ==>  Preparing: SELECT book_id, name, number, detail FROM book WHERE book_id = ? 
20:15:04.047 [main] DEBUG com.hisen.dao.BookDao.queryById - ==> Parameters: 101(Long)
20:15:04.076 [main] DEBUG com.hisen.dao.BookDao.queryById - <==      Total: 1
20:15:04.088 [main] DEBUG org.mybatis.spring.SqlSessionUtils - Closing non transactional SqlSession [[email protected]]
Book{bookId=101, name='活着0', number=100, detail='描述0'}

以此类推,就不多写了

第八步:创建service

├── service
│   ├── BookService.java
│   └── impl
│       └── BookServiceImpl.java

具体内容:service

  1. 创建测试类:BookServiceImplTest
  2. BookServiceImplTest:查看代码BookServiceImplTest.java
  3. 测试:getById
20:19:58.871 [main] DEBUG o.m.s.t.SpringManagedTransaction - JDBC Connection [[email protected]] will not be managed by Spring
20:19:58.885 [main] DEBUG com.hisen.dao.BookDao.queryById - ==>  Preparing: SELECT book_id, name, number, detail FROM book WHERE book_id = ? 
20:19:58.974 [main] DEBUG com.hisen.dao.BookDao.queryById - ==> Parameters: 101(Long)
20:19:59.004 [main] DEBUG com.hisen.dao.BookDao.queryById - <==      Total: 1
20:19:59.011 [main] DEBUG org.mybatis.spring.SqlSessionUtils - Closing non transactional SqlSession [[email protected]]
Book{bookId=101, name='活着0', number=100, detail='描述0'}

第九步:创建controller

└── web
    └── BookController.java

具体内容:BookController.java

第十步:创建jsp页面以及配置web.xml

页面主要使用了bootstrap

└── webapp
    ├── index.jsp
    └── WEB-INF
        ├── jsp
        │   ├── detail.jsp
        │   └── list.jsp
        └── web.xml

具体内容:查看具体内容


Get A Weekly Email With Trending Projects For These Topics
No Spam. Unsubscribe easily at any time.
Java (698,488
Bootstrap (10,160
Redis (6,849
Spring (5,045
Demo (4,293
Maven (3,264
Cache (2,437
Spring Mvc (1,806
Mybatis (1,491
Ssm (417
Mapper (356
Idea (293
Logback (179
Java Book (8
Related Projects