此次整合相比之前的整合少了许多的配置文件,看起来更清晰一点,项目结构如下:
首先是mapper
package com.haochen.dao;
import com.haochen.pojo.User;
//@Mapper
//此处注释注解因为启动类里面写了mapperScan二选一
public interface UserMapper extends tk.mybatis.mapper.common.Mapper<User> {
}
service接口
package com.haochen.service;
import com.haochen.pojo.User;
import java.util.List;
/**
* @author zhang
* @date 2019年03月13日 20:36
*/
public interface UserService {
User getUser(int id);
int save(User user);
void delete(int id);
void modify(User user);
List<User> queryAll();
}
实现类
package com.haochen.service.impl;
import com.haochen.dao.UserMapper;
import com.haochen.pojo.User;
import com.haochen.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
import java.util.List;
/**
* @author zhang
* @date 2019年03月13日 20:37
*/
@Service
@Transactional
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
public User getUser(int id) {
return userMapper.selectByPrimaryKey(id);
//return userMapper.findUserById(id);
}
public int save(User user) {
return userMapper.insert(user);
}
public void delete(int id) {
userMapper.deleteByPrimaryKey(id);
}
public void modify(User user) {
userMapper.updateByPrimaryKey(user);
//int i=1/0;
}
@Override
public List<User> queryAll() {
return userMapper.selectAll();
}
}
controller
package com.haochen.controller;
import com.haochen.pojo.User;
import com.haochen.service.UserService;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author zhang
* @date 2019年03月12日 14:42
*/
@Controller
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/user/{id}")
@ResponseBody
public User getUser(@PathVariable int id){
return userService.getUser(id);
}
@GetMapping("/userList")
public String getUserList(Model model){
List<User> users = userService.queryAll();
model.addAttribute("users",users);
return "hello";
}
}
pojo
package com.haochen.pojo;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;
import tk.mybatis.mapper.annotation.KeySql;
import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.Table;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
/**
* 实体注解详解地址---> https://github.com/abel533/Mapper/wiki/2.2-mapping
*/
//默认开启驼峰映射 此处其实可以不写 这个注解
@Table(name = "user")
@Data
public class User implements Serializable {
@Id
@KeySql(useGeneratedKeys = true)
private Integer id;
@Column
private String username;// 用户姓名
private String sex;// 性别
@DateTimeFormat(pattern="yyyy-mm-dd")
private Date birthday;// 生日
private String address;// 地址
}
启动类
package com.haochen.springbootApp;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import tk.mybatis.spring.annotation.MapperScan;
@SpringBootApplication
@MapperScan(basePackages = "com.haochen.dao")
@ComponentScan(basePackages = "com.haochen")
public class MySpringbootApplication {
public static void main(String[] args) {
SpringApplication.run(MySpringbootApplication.class, args);
}
/*@Bean
public MapperScannerConfigurer mapperScannerConfigurer(){
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
mapperScannerConfigurer.setBasePackage("com.haochen.dao");
return mapperScannerConfigurer;
}*/
}
application.yml
logging:
level:
com.haochen: debug
mybatis:
type-aliases-package: com.haochen.pojo
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
hikari:
username: root
password: root
url: jdbc:mysql://127.0.0.1:3306/mybatis
html页面
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>首页</title>
<style type="text/css">
table {border-collapse: collapse; font-size: 14px; width: 80%; margin: auto}
table, th, td {border: 1px solid darkslategray;padding: 10px}
</style>
</head>
<body>
<div style="text-align: center">
<span style="color: darkslategray; font-size: 30px">欢迎光临!</span>
<hr/>
<table class="list">
<tr>
<th>id</th>
<th>用户名</th>
<th>性别</th>
<th>生日</th>
<th>地址</th>
</tr>
<tr th:each="user : ${users}">
<td th:text="${user.id}">1</td>
<td th:text="${user.username}">zhangsan</td>
<td th:text="${user.sex} == 1 ? '男': '女'">男</td>
<td th:text="${#dates.format(user.birthday, 'yyyy-MM-dd')}">1980-02-30</td>
<td th:text="${user.address}">hhh</td>
</tr>
</table>
</div>
</body>
</html>
启动访问:http://localhost:8080/userList
后台:
前台返回页面自己测试