一、tk mybatis
1、引入依赖
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.0.2</version>
</dependency>
2.注解
@Table(name = "bill_")//要和数据库里的表名一样
@Id//标注主建
@GeneratedValue(strategy = GenerationType.IDENTITY)//主键策略
@Column(name = "id_") //列要和数据库名字里的一样,支持驼峰命名
@Transient //表示该属性不是数据库里的
3.mapper方法
/*使用mapper来对数据库进行操作,注意要导入tk.mybatis.mapper.common.Mapper的包*/
import tk.mybatis.mapper.common.Mapper;
import java.util.List;
public interface BillMapper extends Mapper<Bill> {
}
方法 | 说明 |
---|---|
List select(T record) | 根据实体中的属性值进行查询,查询条件使用等号 |
T selectByPrimaryKey(Object key) | 根据主键字段进行查询,方法参数必须包含完整的主键属性, |
List selectAll() | 查询全部结果,select(null)方法能达到同样的效果 |
T selectOne(T record) | 根据实体中的属性进行查询,只能有一个返回值,多个则抛出异常 |
int selectCount(T record); | 根据实体中的属性查询总数, |
int insert(T record) | 保存一个实体,null的属性也会保存,不会使用数据库默认值 |
int updateByPrimaryKey(T record) | 根据主键更新实体全部字段,null值会被更新 |
int updateByPrimaryKeySelective(T record) | 根据主键更新属性不为null的值 |
int delete(T record) | :根据实体属性作为条件进行删除 |
int deleteByPrimaryKey(Object key) | 根据主键字段进行删除,方法参数必须包含完整的主键属性 |
List selectByExample(Object example) | 根据Example条件进行查询 重点 |
int selectCountByExample(Object example) | 根据Example条件进行查询总数 |
int updateByExample(@Param(“record”) T record, @Param(“example”) Object example) | 根据Example条件更新实体 record 包含的全部属性,null值会被更新 |
int deleteByExample(Object example) | 根据Example条件删除数据 |
4.启动类
import tk.mybatis.spring.annotation.MapperScan;
@SpringBootApplication
@EnableConfigurationProperties
@MapperScan("com.lxs.demo.dao")
public class Application {
二、Thymeleaf
1.引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2.配置
spring:
thymeleaf:
cache: false //关闭模板缓存
prefix: classpath:/templates/
suffix: .html
注意,把html 的名称空间,改成: xmlns:th="http://www.thymeleaf.org" 会有语法提示
3.常用语法
<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>
<th>生日</th>
<th>备注</th>
<th>操作</th>
</tr>
<tr th:each="user, status : ${users}" th:object="${user}">//遍历集合,先用${}表达式来取出user,使用*{}则要用th:object="${user}"
<td th:text="${user.id}">1</td>
<td th:text="*{name}">张三</td>
<td th:text="*{userName}">zhangsan</td>
<td th:text="${user.age}">20</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.note}">1</td>
<td>
<a th:href="@{/delete(id=${user.id}, userName=*{userName})}">删除</a> //url表达式
<a th:href="|/update/${user.id}|">修改</a> //文本替换
<a th:href="'/approve/' + ${user.id}">审核</a> //字符串拼接
</td>
</tr>
</table>
4.常用的th标签
5.内联js
内联文本:[[…]]内联文本的表示方式,使用时,必须先用th:inline=”text/javascript/none”激活,th:inline可以在父级标签内使用,甚至作为body的标签
<h5>内联js</h5>
<script th:inline="javascript">
/*<![CDATA[*/
var text = '[[${text}]]';
alert(text);
/*]]>*/
</script>
6.内嵌变量
<h5>内置变量</h5>
<h6 th:text="${#dates.createNow()}">获取当前日期</h6>
<h6 th:text="${#dates.createNow()}">获取当前日期</h6>
<h6 th:text="${#strings.substring(text, 6, 9)}">截取字符串</h6>
<h6 th:text="${#strings.length(text)}">获得长度</h6>
<h6 th:text="${#strings.randomAlphanumeric(6)}">随机字符串</h6>
<h6 th:text="${#strings.equals(text, 'hello text....')}"></h6>
三、Mybatis Plus
Mybatis-Plus(简称MP)是一个 Mybatis 的增强工具,在 Mybatis 的基础上只做增强不做改变。
1.引入依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatisplus.version}</version>
</dependency>
//引用了这个不用写get、set方法,在实体类加上@Data注解
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
2.启动类
@SpringBootApplication
@MapperScan("com.lxs.quickstart.mapper")
public class QuickstartApplication {
public static void main(String[] args) {
SpringApplication.run(QuickstartApplication.class, args);
}
}
3.dao方法
public interface UserMapper extends BaseMapper<User> {
}
4.常用注解
//如果mysql自增主键注解策略设置如下
@TableId(type = IdType.AUTO)
private Long id;
//排除实体类中非表字段
@TableField(exist = false)
5.内置增删改查
@Test
public void testInsert() {
User user = new User();
user.setName("开吧");
user.setEmail("ls@163.com");
user.setAge(3);
Assert.assertTrue(mapper.insert(user) > 0);
mapper.selectList(null).forEach(System.out :: println);
}
@Test
public void testDelete() {
// //主键删除
// mapper.deleteById(3l);
// mapper.selectList(null).forEach(System.out :: println);
// //批量删除:1
// mapper.delete(new QueryWrapper<User>().like("name", "J"));
// mapper.selectList(null).forEach(System.out :: println);
// //批量删除:2
// mapper.delete(Wrappers.<User>query().like("name", "J"));
// mapper.selectList(null).forEach(System.out :: println);
//批量删除:2
mapper.delete(Wrappers.<User>query().lambda().like(User::getName, "J"));
mapper.selectList(null).forEach(System.out :: println);
}
@Test
public void testUpdate() {
// //基本修改
// mapper.updateById(new User().setId(1l).setName("慧科"));
// mapper.selectList(null).forEach(System.out :: println);
// //批量修改:1
// mapper.update(null, Wrappers.<User>update().set("email", "huike@163.com").like("name",
"J"));
// mapper.selectList(null).forEach(System.out :: println);
//批量修改:2
mapper.update(new User().setEmail("huike@163.com"), Wrappers.<User>update().like("name",
"J"));
mapper.selectList(null).forEach(System.out :: println);
}
@Test
public void testSelect() {
// //基本查询
// System.out.println(mapper.selectOne(Wrappers.<User>query().eq("name", "Tom")));
//投影查询
mapper.selectList(new QueryWrapper<User>().select("id", "name")).forEach(user -> {
System.out.println(user);
});
}
6.内置分页
写配置类
@Configuration
public class MybatisPlusConfig {
/**
* 分页插件
*/
@Bean
public PaginationInterceptor paginationInterceptor() {
// 开启 count 的 join 优化,只针对 left join !!!
return new PaginationInterceptor().setCountSqlParser(new JsqlParserCountOptimize(true));
}
}
@Test
public void testPage() {
System.out.println("------ baseMapper 自带分页 ------");
Page<User> page = new Page<>(1, 5);
IPage<User> pageResult = mapper.selectPage(page, new QueryWrapper<User>().eq("age", 20));
System.out.println("总条数 ------> " + pageResult.getTotal());
System.out.println("当前页数 ------> " + pageResult.getCurrent());
System.out.println("当前每页显示数 ------> " + pageResult.getSize());
pageResult.getRecords().forEach(System.out :: println);
}
7.配置
# 配置mybatis plus
mybatis-plus:
type-aliases-package: com.lxs.crud.entity #别名搜索
mapper-locations: classpath:/mappers/*.xml #加载映射文件
7.pageHelper分页
引入依赖
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.11</version>
</dependency>
配置类
@Configuration
@MapperScan("com.lxs.mybatisplus.samples.crud.mapper")
public class MybatisPlusConfig {
/**
* mp分页插件
*/
@Bean
public PaginationInterceptor paginationInterceptor() {
// 开启 count 的 join 优化,只针对 left join !!!
return new PaginationInterceptor().setCountSqlParser(new JsqlParserCountOptimize(true));
}
/**
* 两个分页插件都配置,不会冲突
* pagehelper的分页插件
*/
@Bean
public PageInterceptor pageInterceptor() {
return new PageInterceptor();
}
使用方法
PageInfo<User> page = PageHelper.startPage(1, 2).doSelectPageInfo(() ->
mapper.selectList(Wrappers.<User>query()));
PageHelper.startPage(1,2);
// PageInfo<User> page = new PageInfo<>(mapper.selectList(Wrappers.<User>query()));
User u = new User();
u.setAge(20);
PageInfo<User> page = new PageInfo<User>(mapper.selectUserByPage2(u))