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

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

springboot整合ssm,使用通用mapper,集成thymeleaf_第1张图片

首先是mapper

package com.haochen.dao;

import com.haochen.pojo.User;

//@Mapper
//此处注释注解因为启动类里面写了mapperScan二选一
public interface UserMapper extends tk.mybatis.mapper.common.Mapper {

}

 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 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 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 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页面




    
    首页
    


欢迎光临!
id 用户名 性别 生日 地址
1 zhangsan 1980-02-30 hhh

启动访问:http://localhost:8080/userList

后台:

前台返回页面自己测试

你可能感兴趣的:(框架整合)