三、博客系统,Spring Boot + MyBatis
MyBatis 中文网站:http://www.mybatis.org/mybatis-3/zh/index.html
mybatis 是一个半自动化的 orm 框架,所谓半自动化就是 mybaitis 只支持数据库查出的数据映射到 pojo 类上,而实体到数据库的映射需要自己编写 sql 语句实现,相较于 hibernate 这种完全自动化的框架我更喜欢 mybatis,mybatis 非常灵活,可以随心所欲的编写自己的 sql 语句来实现复杂的数据库操作,还会有一种畅酣淋漓的编写 sql 语句的潇洒感,但是以前的 mybaits 需要一大堆的配置文件,以及各种 mapper 和 dao 和实体的关联,导致使用 mybatis 还是不够简洁,后来 mybatis 也发现了这个弊端,开发了 mybatis generator 工具来自动化生成实体类、mapper 配置文件、dao 层代码来减轻开发工作量,在后期也是实现了抛弃 mapper 配置文件基于注解的开发模式,直到现在,mybatis 看 spring boot 这么火热,也开发了一套基于 spring boot 的模式:mybatis-spring-boot-starter。
spring boot 简约轻巧的风格正在逐渐被越来越多的厂商及开发者所认可,包括阿里的开源 RPC 框架 dubbo 也准备开发一套对 spring boot 应用的支持(dubbo-spring-boot-starter 启动配置模块)
一、创建 Spring Boot 工程
完整依赖:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency>
<!-- alibaba的druid数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.9</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
二、创建 DAO、SERVICE、CONTROLLER、MODLE 和 MAPPER**
- 数据库创建脚本
create database blog;
use blog;
create table t_user(
id BIGINT NOT NULL PRIMARY KEY AUTO_INCREMENT,
username varchar(50),
password varchar(50),
create_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
);
2.TUser
package com.pigplay.demo.model;
public class TUser {
private Integer id;
private String username;
private String password;
public TUser(Integer id, String username, String password) {
super();
this.id = id;
this.username = username;
this.password = password;
}
public TUser(String username, String password) {
super();
this.username = username;
this.password = password;
}
public TUser(){}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
};
}
3.TUserDao 和 TUserMapper.xml
TUserDao
package com.pigplay.demo.dao;
import java.util.List;
import com.pigplay.demo.model.TUser;
public interface TUserDao {
public void insertUser(TUser user);
public List<TUser> selectUsers();
}
TUserMapper.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.pigplay.demo.dao.TUserDao" >
<resultMap id="BaseResultMap" type="com.pigplay.demo.model.TUser" >
<id column="id" property="id" jdbcType="INTEGER" />
<result column="username" property="username" jdbcType="VARCHAR" />
<result column="password" property="password" jdbcType="VARCHAR" />
</resultMap>
<sql id="Base_Column_List" >
id, username, password
</sql>
<insert id="insertUser" parameterType="com.pigplay.demo.model.TUser">
INSERT INTO t_user (username, password) values(#{username}, #{password})
</insert>
<select id="selectUsers" resultType="com.pigplay.demo.model.TUser">
select
<include refid="Base_Column_List" />
from t_user
</select>
</mapper>
4.UserController
package com.pigplay.demo.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.github.pagehelper.PageHelper;
import com.pigplay.demo.dao.TUserDao;
import com.pigplay.demo.model.TUser;
import com.pigplay.demo.service.TUserService;
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private TUserService userService;
@RequestMapping("/list")
public String listUsers(Model model, Integer page) {
page = page == null ? 1 : page;
PageHelper.startPage(page, 2);
List<TUser> u = userService.selectUsers();
model.addAttribute("users", u);
return "/user/list";
}
@RequestMapping("/add")
public String add() {
return "/user/add";
}
@RequestMapping("/save")
public String save(Model model, String username, String password) {
userService.insertUser(new TUser(username, password));
PageHelper.startPage(1, 2);
model.addAttribute("users", userService.selectUsers());
return "/user/list";
}
}
三、页面
1.add.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8" />
<title>Insert title here</title>
</head>
<body>
<h1>添加用户</h1>
<form action="/user/save" method="post">
<table>
<tr>
<td>用户名</td><td><input name="username" /></td>
</tr>
<tr>
<td>密码</td><td><input name="password" /></td>
</tr>
</table>
<input type="submit" value="保存" />
</form>
</body>
</html>
2.list.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8" />
<title>Insert title here</title>
<script type="text/javascript">
function pager(p){
window.location.href="/user/list?page=" + p
}
</script>
</head>
<body>
<h1>用户列表</h1>
<div>
<ul>
<li>
<span>id</span>-
<span>用户名</span>-
<span>密码</span>
</li>
<li th:each="user:${users}">
<span th:text="${user.id}"></span>-
<span th:text="${user.username}"></span>-
<span th:text="${user.password}"></span>
</li>
</ul>
<input type="button" onclick="pager(1)" value="上一页" />
<input type="button" onclick="pager(2)" value="下一页" />
</div>
</body>
</html>
四、application.yml
spring:
thymeleaf:
cache: false
suffix: .html
datasource:
name: test
url: jdbc:mysql://127.0.0.1:3306/blog
username: root
password: md123
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
filters: stat
maxActive: 20
initialSize: 1
maxWait: 60000
minIdle: 1
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: select 'x'
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxOpenPreparedStatements: 20
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.pigplay.demo.model
#pagehelper 分页插件
pagehelper:
helperDialect: mysql
reasonable: true
supportMethodsArguments: true
params: count=countSql