SpringBoot 2.0 mybatis mapper通用类

 <!---mybatis通用类包含mybatis和连接池  mybatis和连接池就不需要引入-->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.1.2</version>
</dependency>

  

接口类:

package cn.itcast.mapper;

import cn.itcast.pojo.User;
import tk.mybatis.mapper.common.Mapper; public interface UserMapper extends Mapper<User> {
}

  

实体类:

package cn.itcast.pojo;

import lombok.Data;
import tk.mybatis.mapper.annotation.KeySql; import javax.persistence.Id;
import javax.persistence.Table;
import java.util.Date; @Data
@Table(name = "user")
public class User {
@Id //id
@KeySql(useGeneratedKeys = true) //主键自增
private Integer id; private String mobile; private String nickName; private String password; private String salt; private String head; private Date registerDate; private Date lastLoginDate; private Integer loginCount;
}

  

配置:

server:
port: 8887
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8&serverTimezone=UTC
username: root
password:
mybatis:
#configuration:
#map-underscore-to-camel-case: true
mapper-locations: mapper/*.xml
type-aliases-package: cn.itcast.pojo

  

入口文件:

import tk.mybatis.spring.annotation.MapperScan;
@MapperScan("cn.itcast.mapper")
package cn.itcast;

import cn.itcast.interceptor.MyInterceptor;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import tk.mybatis.spring.annotation.MapperScan; @SpringBootApplication
@MapperScan("cn.itcast.mapper")
public class BootDemoApplication implements WebMvcConfigurer {
//psvm 快速创建main
public static void main(String[] args) {
SpringApplication.run(BootDemoApplication.class,args);
} @Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**");
}
}

  

UserService:

package cn.itcast.service;

import cn.itcast.pojo.User;

public interface UserService {
public User queryById(Long id); public Integer insertUser(User user);
}

  

UserServiceImpl:

package cn.itcast.service;

import cn.itcast.mapper.UserMapper;
import cn.itcast.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; @Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper; public User queryById(Long id){
return userMapper.selectByPrimaryKey(id);
} @Transactional
public Integer insertUser(User user){
return userMapper.insert(user);
}
}

  

controller文件:

package cn.itcast.web;

import cn.itcast.pojo.User;
import cn.itcast.service.UserService;
import cn.itcast.service.UserServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; import javax.sql.DataSource; @Slf4j
@RestController
@RequestMapping("user")
public class HelloController {
@Autowired
private UserService userService; /*@Autowired
private DataSource dataSource;
*/
@GetMapping("index")
public User Hello(@RequestParam("id") Long id){
return userService.queryById(id);
}
}

  

打开浏览器,访问:

http://localhost:8887/user/index?id=1

{
"id": 1,
"mobile": "13851848299",
"nickName": "william",
"password": "29ec3cef080fd52f406eb5ec30c7efba",
"salt": "1A2B3C4D",
"head": null,
"registerDate": null,
"lastLoginDate": null,
"loginCount": null
}

上一篇:利用https实现站点的访问


下一篇:Android WebView的HTML中的select标签不起作用