学习SpringBoot,通过Springboot简单配置连接Mysql数据库,采用Mybatis注解方式实现数据库“增、删、改、查”,结合Spring web实现页面呈现后,。
IDE及相关环境:Windows10 :Eclipse+Maven,Linux(Centos7):Mysql 7.5
4.0.0
springboot
springboot-mybatis-annotation
0.0.1-SNAPSHOT
jar
org.springframework.boot
spring-boot-starter-parent
1.5.9.RELEASE
1.2.0
5.1.39
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-thymeleaf
org.springframework.boot
spring-boot-starter-test
test
org.mybatis.spring.boot
mybatis-spring-boot-starter
${mybatis-spring-boot}
mysql
mysql-connector-java
org.projectlombok
lombok
provided
com.github.pagehelper
pagehelper-spring-boot-starter
1.2.5
junit
junit
org.projectlombok
lombok-maven-plugin
1.12.6.0
此处说明一下lombok,主要是为了省略实体定义中的重复set、get定义方法,具体可百度相关资料不少。
spring.datasource.url=jdbc:mysql://xxx.xxx.xxx.xxx/database?useUnicode=true&characterEncoding=utf8&useSSL=false
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
// xxx.xxx.xxx.xxx/database 为mysql数据库所在地址及相应数据库(本文中mysql不在本机,在linux服务器中,因此需要IP地址)
server.port=8088 //配置tomcat端口(默认为8080,已被其他app占用)
spring.thymeleaf.cache=false //设置thymeleaf缓存机制(thymeleaf为快速开发web引擎模板,目前相关资料较少,后续考虑更换采用bootstrap编写前端)
xxx.xxx.xxx.xxx/database?useUnicode=true&characterEncoding=utf8&useSSL=false
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
// xxx.xxx.xxx.xxx/database 为mysql数据库所在地址及相应数据库(本文中mysql不在本机,在linux服务器中,因此需要IP地址)
server.port=8088 //配置tomcat端口(默认为8080,已被其他app占用)
spring.thymeleaf.cache=false //设置thymeleaf缓存机制(thymeleaf为快速开发web引擎模板,目前相关资料较少,后续考虑更换采用bootstrap编写前端)
package com.test.user.entity;
import java.io.Serializable;
import java.sql.Date;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import lombok.Getter;
import lombok.Setter;
/**
* User实体类
*
*/
@Setter
@Getter
@JsonAutoDetect
public class User implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* UserID编号
*/
private String id;
/**
* UserName用户名称
*/
private String name;
/**
* UserBirth用户生日
*/
private Date birthday;
/**
* 描述
*/
private String description;
@Override
public String toString(){
return "User{"+
"id="+id+
",name="+name+
",birthday="+birthday+
",description="+description+"}";
}
编写User实体类,对应mysql数据库中的testuser表;引入了lombok因此省略set,get方法的定义。
相应的数据库testuser表结构如下:
CREATE TABLE `testuser` (
`UserID` varchar(50) NOT NULL,
`UserName` varchar(50) DEFAULT NULL,
`UserBirth` date DEFAULT NULL,
`Descript` varchar(255) DEFAULT NULL,
PRIMARY KEY (`UserID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
package com.test.user.dao;
import java.util.List;
import org.apache.ibatis.annotations.*;
import com.test.user.entity.User;
/**
* User DAO 接口类
*
*/
@Mapper // 标志为 Mybatis 的 Mapper
public interface UserDao {
/**
* 列出所有用户信息
*
* @param name 用户名
*/
@Select("SELECT * FROM testuser ORDER BY UserID")
// 返回所有User信息
@Results({
@Result(property = "id", column = "UserID"),
@Result(property = "name", column = "UserName"),
@Result(property = "birthday", column = "UserBirth"),
@Result(property = "description", column = "Descript")})
List showAll();
/**
* 根据UserName查询用户信息
* @param name
* @return
*/
@Select("SELECT * FROM testuser WHERE UserName=#{name}")
//根据UserName查询User信息
@Results({
@Result(property = "id", column = "UserID"),
@Result(property = "name", column = "UserName"),
@Result(property = "birthday", column = "UserBirth"),
@Result(property = "description", column = "Descript")})
List selectByName(String name);
//根据UserID查询User信息
@Select("SELECT * FROM testuser WHERE UserID=#{id}")
@Results({
@Result(property = "id", column = "UserID"),
@Result(property = "name", column = "UserName"),
@Result(property = "birthday", column = "UserBirth"),
@Result(property = "description", column = "Descript")})
User selectByID(String id);
/**
* 插入新User
* @param user
* @return
*/
@SelectKey(keyProperty = "id",keyColumn="UserID",resultType = String.class, before =true, statement = "select replace(uuid(), '-', '-')" )
@Options(keyProperty = "id", useGeneratedKeys = true)
@Insert("INSERT INTO testuser(UserID,UserName,UserBirth,Descript)"
+ "VALUES(#{id},#{name},#{birthday},#{description})")
// @Results({
// @Result(property = "id", column = "UserID"),
// @Result(property = "name", column = "UserName"),
// @Result(property = "birthday", column = "UserBirth"),
// @Result(property = "description", column = "Descript")})
public void addUser(User user);
/**
* 根据用户ID更新用户信息(ID唯一标识)
* @param user
*/
@Update("UPDATE testuser SET UserName=#{name},UserBirth=#{birthday},Descript=#{description} WHERE UserID=#{id}")
@Results({
@Result(property = "id", column = "UserID"),
@Result(property = "name", column = "UserName"),
@Result(property = "birthday", column = "UserBirth"),
@Result(property = "description", column = "Descript")})
public void updateByUserID(User user);
/**
* 根据用户ID删除用户信息(ID唯一标识)
* @param id
* @return
*/
@Delete("DELETE FROM testuser WHERE UserID=#{id}")
public void deleteByUserID(String id);
}
这里采用Mybatis注解的方式引入数据库增删改查的操作,省去xml配置的书写(取决于个人喜好,不习惯采用xml配置的方式)
package com.test.user.service;
import java.util.List;
import com.test.user.entity.User;
/**
* User业务逻辑接口类
*
*/
public interface UserService {
/**
* 列出所有用户信息
*/
List showAll();
/**
* 根据用户名称查询用户信息
*/
List selectByName(String name);
/**
* 增加用户信息
* @return
*/
User selectByID(String id);
// User selectByName(String name);
public void addUser(User user);
/**
* 根据UserID更新用户信息
*/
public void updateByUserID(User user);
// /**
// * 根据UserName更新用户信息
// */
// public void updateByUserName(User user);
/**
* 根据UserID删除用户信息
* @param id
* @return
*/
public void deleteByUserID(String id);
/**
* 根据UserName删除用户信息
* @param name
* @return
*/
// public void deleteByUserName(String name);
//
}
UserService类,定义了增删改查的方法(貌似可以省去,直接由UserServiceImpl实现UserDao类也可以实现完整功能)。
package com.test.user.service.impl;
import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import com.test.user.dao.UserDao;
import com.test.user.entity.User;
import com.test.user.service.UserService;
/**
* UserService逻辑实现类
*
*/
@Service("UserService")
public class UserServiceImpl implements UserService {
private static final Logger LOGGER= LoggerFactory.getLogger(UserServiceImpl.class);
@Autowired
private UserDao userDao;
@Resource
private RedisServiceImpl redisService;
/**
* 获取User逻辑:如缓存存在则从换从取值,否则从DB中获取,插入缓存
*/
public List showAll() {
return userDao.showAll();
}
public List selectByName(String name){
return userDao.selectByName(name);
}
public User selectByID(String id) {
User user=userDao.selectByID(id);
return user;
}
public void addUser(User user){
userDao.addUser(user);
}
public void updateByUserID(User user) {
userDao.updateByUserID(user);
}
public void deleteByUserID(String id) {
userDao.deleteByUserID(id);
}
}
UserServiceImpl类,实现Service类中定义的方法,此处@Cache相关注解参考了别人的blog,暂时还未全部搞懂原理。
package com.test.user.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.test.user.entity.User;
import com.test.user.service.UserService;
/**
*
*/
@Controller
@RequestMapping(value="/user")
public class UserRestController {
@Autowired
private UserService userService;
private static final String USER_LIST = "UserList";
private static final String USER_LIST2 = "UserList2";
private static final String USER_FORM = "UserForm";
private static final String REDIRECT_TO_USER_LIST = "redirect:/user";
/**
* 获取用户列表
* 通过"/user"GET请求获取
* @param map
* @return
*/
@RequestMapping(method=RequestMethod.GET)
public String showAll(ModelMap map) {
map.addAttribute("UserList",userService.showAll());
return USER_LIST;
}
/**
*
* @param name
* @return
*/
@RequestMapping(value="/selectByName/{name}",method=RequestMethod.GET)
public String selectByUserName(@PathVariable String name, ModelMap map){
if(userService.selectByName(name)==null) {
return "传值未成功!";
}
else {
map.addAttribute("UserList2",userService.selectByName(name));
return USER_LIST2;
}
}
@RequestMapping(value="/selectByID/{id}",method=RequestMethod.GET)
public String selectByID(@PathVariable String id, ModelMap map){
if(userService.selectByID(id)==null) {
return "传值未成功!";
}
else {
map.addAttribute("UserList2",userService.selectByID(id));
return USER_LIST2;
}
}
/**
* 获取创建UserForm表单
* @param map
* @return
*/
@RequestMapping(value="/addUser",method=RequestMethod.GET)
public String createUserForm(ModelMap map) {
map.addAttribute("user", new User());
map.addAttribute("action", "addUser");
return USER_FORM;
}
@RequestMapping(value="/addUser",method=RequestMethod.POST)
public String postUser(@ModelAttribute User user) {
userService.addUser(user);
return REDIRECT_TO_USER_LIST;
}
@RequestMapping(value="/updateByID/{id}",method=RequestMethod.GET)
public String getByUserID(@PathVariable String id, ModelMap map) {
if(userService.selectByID(id)==null) {
return "传值未成功!";
}
else
{ map.addAttribute("user",userService.selectByID(id));
map.addAttribute("action","updateByID");
return USER_FORM;
}
}
@RequestMapping(value="/updateByID",method=RequestMethod.POST)
public String putByUserID(@ModelAttribute User user) {
userService.updateByUserID(user);
return REDIRECT_TO_USER_LIST;
}
@RequestMapping(value="/deleteByID/{id}",method=RequestMethod.GET)
public String deleteUserByID(@PathVariable String id) {
userService.deleteByUserID(id);
return REDIRECT_TO_USER_LIST;
}
}
package com.test.user;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableAutoConfiguration
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
至此所有后端相关的类已全部定义,运行程序可通过POSTMan等API工具实现增删改查操作。(前端相关的不是很懂,尝试结合别人写的Demo做了简单页面)
在resource文件下建立如下文件夹及相关文件(其中UserForm1.html及UserInfoByName.html无用,为实现一些小想法做的测试);
1.static/css/default.css
/* contentDiv */
.contentDiv {padding:50px 100px;}
css模板引入?不是很懂。。。
2.image引入
此处引入的是作为标签页的logo-----> 就是这个东西lol。本文中页面没有添加图片及背景之类的,因此就引入了一张图片作为标签页图像。
3.静态页面编写
UserList.html用于列出查询的所有用户(showAll),UserForm.html页面为添加(add)、更新(updata)用户的跳转页面,UserList2.html页面用于列出根据条件查询所得用户信息的列表页面,代码基本与UserList相同——取消了几个按钮的设置。
1.UserList.html
用户列表
用户编号 | 用户名称 | 用户生日 | 用户简介 | 附件 | ||
---|---|---|---|---|---|---|
附件 | 删除 | 更新 |