一、目录
1.数据库表user.sql文件
/*
Navicat Premium Data Transfer
Source Server : vmroot
Source Server Type : MySQL
Source Server Version : 80013
Source Host : 127.0.0.1:3306
Source Schema : springboot
Target Server Type : MySQL
Target Server Version : 80013
File Encoding : 65001
Date: 21/04/2019 18:35:24
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`user_name` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '用户名',
`password` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '密码',
`name` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '姓名',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 7 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES (1, 'zhangsan', '123456', '张三');
INSERT INTO `user` VALUES (2, 'lisi', '123456', '李四');
INSERT INTO `user` VALUES (3, 'wangwu', '123456', '王五');
INSERT INTO `user` VALUES (4, 'zhangwei', '123456', '张伟');
INSERT INTO `user` VALUES (5, 'lina', '123456', '李娜');
INSERT INTO `user` VALUES (6, 'lilei', '123456', '李磊');
SET FOREIGN_KEY_CHECKS = 1;
2.pom.xml的依赖
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.lcx</groupId>
<artifactId>springboot-mybatis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- Inherit defaults from Spring Boot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
</parent>
<!-- Add typical dependencies for a web application -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- spring和mybatis整合依赖 -->
<!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!-- 导入redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
<version>1.3.8.RELEASE</version>
</dependency>
<!-- 导入测试 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope></scope>
</dependency>
</dependencies>
</project>
3.application.properties配置文件,jpa的配置要配置hibernate的东西
#DB
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/springboot
spring.datasource.username=root
spring.datasource.password=root
#jpa
spring.jpa.database=mysql
spring.jpa.show-sql=true
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy
4.实体类user.java,用mybatis整合user_name要注意
package cn.lcx.domain;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
/**
* 用户实体类
* @author kuxin
*
*/
@Entity
@Table(name="user")
public class User implements Serializable {
/**
*
*/
private static final long serialVersionUID = 8703643314757223600L;
@Id
@Column(name="id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
@Column(name="user_name")
private String user_name;
@Column(name="password")
private String password;
@Column(name="name")
private String name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUser_name() {
return user_name;
}
public void setUser_name(String user_name) {
this.user_name = user_name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User [id=" + id + ", user_name=" + user_name + ", password=" + password + ", name=" + name + "]";
}
}
5.使用mybatis实现对数据库的操作接口IUserMapper
package cn.lcx.dao;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import cn.lcx.domain.User;
/**
* 使用mybatis实现对数据库的操作接口
* @author kuxin
*
*/
@Mapper//要求mybatis 的版本是3.3及以上
public interface IUserMapper {
@Select("select * from user where name like '%${value}%'")
public List<User> findUserByName(String name);
@Select("select * from user")
public List<User> findAll();
}
6.用户的业务层实现类UserServiceImpl,
redis的整合 1.引导类@EnableCaching//开启springboot对缓存的支持,
2.业务层方法上@Cacheable(value="findAll")//表示当前的方法使用缓存,并存入redis数据库中
package cn.lcx.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import cn.lcx.dao.IUserMapper;
import cn.lcx.domain.User;
import cn.lcx.service.IUserService;
/**
* 用户的业务层实现类
* @author kuxin
*
*/
@Service("userService")
public class UserServiceImpl implements IUserService {
// @Autowired
// private IUserDao userDao;//spring data jpa的实现
@Autowired
private IUserMapper userMapper;
@Override
public List<User> findAllUserByName(String name) {
return userMapper.findUserByName(name);
}
@Override
// @Cacheable(value="findAllUserCache",key="user.findAllUser")//表示当前的方法使用缓存,并存入redis数据库中
//value属性:表示存入redis数据库的key
//key属性:用于指定方法执行返回值的key,该属性是spring用的。不用也有默认值
@Cacheable(value="findAll")
public List<User> findAllUser() {
System.out.println("执行去数据库查询");
return userMapper.findAll();
}
}
7. 用户的业务层接口IUserService
package cn.lcx.service;
import java.util.List;
import cn.lcx.domain.User;
/**
* 用户的业务层接口
* @author kuxin
*
*/
public interface IUserService {
/**
* 根据名称查询所有用户
* @return
*/
List<User> findAllUserByName(String name);
/**
* 查询所有用户
* @return
*/
List<User> findAllUser();
}
8.用户的控制层UserController
package cn.lcx.web.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.lcx.domain.User;
import cn.lcx.service.IUserService;
/**
* 用户的控制层
* @author kuxin
*
*/
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private IUserService userService;
@RequestMapping("/findAll/{name}")
public List<User> findAllUserByName(@PathVariable("name")String name){
List<User> users = userService.findAllUserByName(name);
return users;
}
@RequestMapping("/findAll")
public List<User> findAllUser(){
List<User> users = userService.findAllUser();
return users;
}
}
9.引导类Application
package cn.lcx;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
/**
* spring boot 的引导类
* @author kuxin
*
*/
@SpringBootApplication
@EnableCaching//开启springboot对缓存的支持
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
10.user.html页面
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>人员信息</title>
<link rel="stylesheet" type="text/css" href="easyui/themes/default/easyui.css">
<link rel="stylesheet" type="text/css" href="easyui/themes/icon.css">
<script type="text/javascript" src="easyui/jquery.min.js"></script>
<script type="text/javascript" src="easyui/jquery.easyui.min.js"></script>
<script type="text/javascript" src="easyui/locale/easyui-lang-zh_CN.js"></script>
</head>
<body>
<h2>人员信息</h2>
<div style="margin:20px 0;"></div>
<table class="easyui-datagrid" title="人员信息" style="width:500px;height:250px"
data-options="singleSelect:true,collapsible:true,url:'/user/findAll',method:'get'">
<thead>
<tr>
<th data-options="field:'id',width:80">ID</th>
<th data-options="field:'user_name',width:100">username</th>
<th data-options="field:'password',width:80,align:'right'">password</th>
<th data-options="field:'name',width:60,align:'center'">name</th>
</tr>
</thead>
</table>
</body>
</html>
二、启动效果
三、Springboot整合junit
package cn.lcx.test;
import java.util.List;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.env.Environment;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import cn.lcx.Application;
import cn.lcx.domain.User;
import cn.lcx.service.IUserService;
/**
* Springboot整合junit
* @author kuxin
*
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Application.class)//属性:用于指定引导类
public class SpringBootJunitTest {
@Autowired
private IUserService userService;
@Test
public void testFindAll() {
List<User> findAllUser = userService.findAllUser();
System.out.println(findAllUser);
}
@Resource
private Environment env;
@Test
public void testReadSpringBootConfig() {
System.out.println(env.getProperty("spring.datasource.url"));
}
}
测试结果在控制台显示