最近一直在学springboot和Cloud,互联网公司现在也更倾向于微服务这一块,前景是一篇光明的,特别是在springboot上开发的Cloud的部分,是一套分布式的整体解决方案,学好这一块至少这几年都很吃香;
既然学习很久,落地实践一下为好;
项目git网址:https://github.com/David-BIQI/manage.git(项目使用比较新的springboot2.0 还有jdk8 )
参照的代码规范:https://github.com/xwjie/PLMCodeTemplate.git (这个是一套能够落地的代码规范,跟着风哥学习很多)
项目已经初始化,连接数据库了,配置mybatis tk框架,加上分页插件<有空还要连接一下mybatis的原理,还有分页的实现原理>
application.yml下 mybatis配置:
# mybatis包的扫描,还有就是映射文件设置 mybatis tk插件的使用
mybatis:
type-aliases-package: package com.biqi.model
mapper-locations: classpath:mapper/*.xml
#配置驼峰下划线
configuration:
map-underscore-to-camel-case: true
pom中的架包:
<!--mybatis tk框架-->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>1.2.4</version>
</dependency>
<!---分页插件-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.3</version>
</dependency>
<!---mybatis tk框架-->
tk实现的例子:
1、定义一个接口
package com.common.mybatis;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.MySqlMapper;
/**
* Description: 实现增删改查的基本sql功能
* @Package com.common.mybatis
* @author xiebq @date 2018年6月7日 上午9:53:34
*/
public interface MyMapper<T> extends Mapper<T>, MySqlMapper<T> {
//FIXME 特别注意,该接口不能被扫描到,否则会出错
}
dao继承接口
package com.biqi.dao;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import com.biqi.model.User;
import com.common.mybatis.MyMapper;
/**
* @Package com.biqi.dao
* @author xiebq @date 2018年6月7日 上午9:55:30
*/
@Mapper
public interface UserDao extends MyMapper<User> {
/**
* 测试数据库连接
* @return
*/
@Select("SELECT count(*) FROM User ")
Integer countUser();
int countUser2();
}
具体使用
public User getUserByid(Integer id) {
User user = userDao.selectByPrimaryKey(id);
return user;
}
public Integer saveUser(User user) {
user.setId(null);
user.setCreateby(SuperUserId);
user.setCreated(new Date());
userDao.insertUseGeneratedKeys(user);
return user.getId();
}
public Boolean deleteUser(Integer id) {
User oldUser = userDao.selectByPrimaryKey(id);
notNull(oldUser, "用户id:"+id+" 不存在");
userDao.deleteByPrimaryKey(id);
return true;
}
分页的例子:
public PageDto<User> listPage(Integer page, Integer size) {
//tk mybatis进行查询
Example example = new Example(User.class);
//分页查询
PageHelper.startPage(page, size);
//添加查询条件
//example.createCriteria().andEqualTo("id",XX);
example.orderBy("created").desc();
List<User> list = userDao.selectByExample(example);
//分页查询-->若凡在这里,不能进行分页 PageHelper.startPage(page, size);
PageInfo<User> pageInfo = new PageInfo<User>(list);
return new PageDto<>(list, pageInfo.getTotal());
}
xml文件<不喜欢在接口中写sql语句的>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.biqi.dao.UserDao">
<select id="countUser2" resultType="java.lang.Integer">
SELECT count(*) FROM User
</select>
</mapper>
以上这些步骤做好单表的增删改查,以及分页基本完成。以下是tk能够完成以下的查询,
来源:oschina
链接:https://my.oschina.net/u/4393418/blog/3285018