springboot整合ssm,使用通用mapper,集成thymeleaf

此次整合相比之前的整合少了许多的配置文件,看起来更清晰一点,项目结构如下:

首先是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

后台:

前台返回页面自己测试

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值