SpringBoot 2.0 mybatis mapper通用类
程序员文章站
2022-03-30 22:15:04
接口类: 实体类: 配置: 入口文件: UserService: UserServiceImpl: controller文件: 打开浏览器,访问: http://localhost:8887/user/index?id=1 { "id": 1, "mobile": "13851848299", "n ......
<!---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);
}
}
打开浏览器,访问:
{
"id": 1,
"mobile": "13851848299",
"nickname": "william",
"password": "29ec3cef080fd52f406eb5ec30c7efba",
"salt": "1a2b3c4d",
"head": null,
"registerdate": null,
"lastlogindate": null,
"logincount": null
}
上一篇: 这个代码的可复用原则是好还是坏?
下一篇: thinkPHP5.0 URL路由优化
推荐阅读
-
详解Mybatis通用Mapper介绍与使用
-
详解Mybatis通用Mapper介绍与使用
-
SpringBoot_02通用mapper
-
mybatis-generator 自动生成mapper以及实体类
-
扩展mybatis和通用mapper,支持mysql的geometry类型字段
-
DB数据源之SpringBoot+MyBatis踏坑过程(二)手工配置数据源与加载Mapper.xml扫描
-
SpringBoot_02通用mapper
-
MyBatis通用Mapper和PageHelper的过程详解
-
Springboot集成通用Mapper与Pagehelper,实现mybatis+Druid的多数据源配置
-
SpringBoot集成mybatis(配置通用mapper)并且使用druid作为数据库连接池