<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>1.5.2.RELEASEversion>
<type>pomtype>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
<version>1.5.2.RELEASEversion>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>1.1.1version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.21version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-thymeleafartifactId>
<version>1.5.2.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<version>1.5.2.RELEASEversion>
<scope>testscope>
dependency>
<dependency>
<groupId>tk.mybatisgroupId>
<artifactId>mapper-spring-boot-starterartifactId>
<version>1.1.5version>
dependency>
<dependency>
<groupId>com.github.pagehelpergroupId>
<artifactId>pagehelper-spring-boot-starterartifactId>
<version>1.2.5version>
dependency>
dependencies>
//数据原配置
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
//通用mapper的配置
mybatis.mapper-locations=classpath:mapper/*.xml #扫描xml文件
mybatis.type-aliases-package=com.lcf.model #扫描model层
mapper.mappers=com.MyMapper #自定义mapper
mapper.not-empty=false
mapper.identity=MYSQL
//分页插件配置
pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
pagehelper.params=count=countSql
package com.lcf;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
@SpringBootApplication
@MapperScan(basePackages="com.lcf.mapper") //扫描Mapper接口所在的包,等同在Mapper接口添加@Mapper
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
实体类设计:
@Table(name = "users")
public class User {
@Id
@Column(name = "id")
private int id;
@Column(name = "name")
private String name;
@Column(name = "age")
private int age;
@Column(name = "birthday")
private Date birthday;
//这里忽略set,get方法
/**
* @author : lichenfei
* @date : 2018年11月29日
* @time : 上午9:43:06
*
*/
package com;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.MySqlMapper;
/**
* @author : lichenfei
* @date : 2018年11月29日
* @time : 上午9:43:06
*
*/
public interface MyMapper<T> extends Mapper<T>, MySqlMapper<T> {
//此类不能被扫描到,负责会出错
}
第六步:mapper接口
/**
* @author : lichenfei
* @date : 2018年11月28日
* @time : 下午7:24:48
*
*/
package com.lcf.mapper;
import java.util.List;
import java.util.Map;
import com.MyMapper;
import com.lcf.model.User;
/**
* @author : lichenfei
* @date : 2018年11月28日
* @time : 下午7:24:48
*
*/
public interface UserMapper extends MyMapper<User> {
/** 根据条件进行查询 */
public List<User> findUserByCondition(Map<String, Object> map);//此方法在后边的分页中会用到
}
第7步:这里忽略service层,直接使用controller 层进行测试
/**
* @author : lichenfei
* @date : 2018年11月29日
* @time : 上午9:57:00
*
*/
package com.lcf.controller;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lcf.mapper.UserMapper;
import com.lcf.model.User;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;
/**
* @author : lichenfei
* @date : 2018年11月29日
* @time : 上午9:57:00
*
*/
@Controller
public class MapperController {
// 通用mapper的测试类
@Autowired
private UserMapper userMapper;
@RequestMapping("addUser")
@ResponseBody
public String addUser() {
// 添加
User user = new User();
user.setAge(12);
user.setBirthday(new Date());
// user.setName("添加测试");
// userMapper.insert(user);//添加的时候实体类中如果没有设置值,则会进行null值添加,不会使用数据库默认值,如果主键为null不收影响,
// userMapper.insertList(null);//批量插入
int num = userMapper.insertSelective(user);// 建议使用,会使用数据库默认的值,null值不会修改,测试的时候int类型没有使用默认值,varchar使用了默认值
// userMapper.insertUseGeneratedKeys(user);
return "添加" + num;// 返回添加的条数
}
@RequestMapping("deleteUser")
@ResponseBody
public String deleteUser() {
// 删除
User user = new User();
user.setName("张三");
// int delete = userMapper.delete(user);没有测试出来
// int delete = userMapper.deleteByPrimaryKey(4);//根据id删除,返回删除的条数
Example ex = new Example(User.class);
Criteria criteria = ex.createCriteria();
criteria.andEqualTo("name", "添加测试");// 根据name=添加测试的进行删除,有多个会一起删除
int delete = userMapper.deleteByExample(ex);// 返回删除的条数
return "删除" + delete;
}
@RequestMapping("updateUser")
@ResponseBody
public String updateUser() {
// 修改
int update = 0;
User user = new User();
user.setAge(100);
user.setBirthday(new Date());
user.setName("100");
// update =
// userMapper.updateByPrimaryKeySelective(user);//根据主键更新实体类数据,建议使用,更新实体类中不为null的值//返回修改的条数
Example ex = new Example(User.class);
Criteria criteria = ex.createCriteria();
criteria.andEqualTo("name", "默认");
update = userMapper.updateByExampleSelective(user, ex);// 根据ex条件进行修改,有几条修改几条,将满足ex条件的数据按照user中的属性进行修改,//返回修改的条数
return "修改" + update;
}
@RequestMapping("findUser")
@ResponseBody
public List<User> findUser() {
// 查询
List<User> users = new ArrayList<User>();
User user = new User();
// user.setAge(100);
user.setName("李四");
// users =
// userMapper.select(user);//根据实体类中的属性进行查询,传入null值等于查询所有,传入实体类时出现问题======
// users = userMapper.selectAll();//查询所有
// User u = userMapper.selectOne(user);//查询单个,只能返回一个,否则会出错,出错===========
// users.add(u);
// User u = userMapper.selectByPrimaryKey(1);//根据主键进行查询
Example ex = new Example(User.class);
Criteria criteria = ex.createCriteria();
criteria.andEqualTo("name", "李四");
// users = userMapper.selectByExample(ex);//根据ex条件进行查询
int num = userMapper.selectCount(null);// 查询总数
System.out.println(num);
// users.add(u);
return users;
}
@RequestMapping("/findUserByPage")
@ResponseBody
public List<User> findUserByPage() {
// 分页
List<User> users = new ArrayList<User>();
PageHelper.startPage(2, 3);// 第一页,每页展示3条数据
// PageHelper.startPage(2, 3, "id desc");//按照id降序排序,这个比较奇葩,先把数据数据进行降序排序,在进行分页查询
users = userMapper.selectAll();
return users;
}
@RequestMapping("/findUserByPageAndCondition")
@ResponseBody
public PageInfo<User> findUserByPageAndCondition() {
// 按照条件进行分页查询
Map<String, Object> map = new HashMap<String, Object>();
map.put("name", "100");// 这里要注意一下,当数据库只有3条数据的时候,而你又要一页展示4条数据,那么在第二页本来应该是没有数据的,但是你非要设置页码为2,那么就展示第一页的数据.
PageHelper.startPage(2, 3);
List<User> list = userMapper.findUserByCondition(map);
PageInfo<User> info = new PageInfo<User>(list);
return info;
}
}
注:mapper.xml(根据条件查询分页)
<mapper namespace="com.lcf.mapper.UserMapper">
<select id="findUserByCondition" parameterType="map"
resultType="com.lcf.model.User">
select * from users
<where>
<if test="name != null and name != ''">//这里只是进行了一个属性的简单演示
and name = #{name}
if>
where>
select>
mapper>
package maventest;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import com.alibaba.fastjson.JSON;
import com.lcf.App;
import com.lcf.mapper.UserMapper;
import com.lcf.model.User;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;
/**
* @author : lichenfei
* @date : 2019年1月2日
* @time : 上午9:24:36
*
*/
@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration(classes = App.class) // 启动类
public class MapperDemo {
@Autowired
private UserMapper userMapper;
@Test
public void method1() {
Example exa = new Example(User.class); // entityClass:实体类.
Criteria criteria = exa.createCriteria();
//criteria.andEqualTo("name","deef");// name = 'deef'
// [{"age":54,"birthday":1545998303000,"id":5,"name":"deef"}]
//criteria.andBetween("age", 34, 46); //在什么之间
//[{"age":34,"birthday":1545307110000,"id":6,"name":"34"},{"age":34,"birthday":1545307119000,"id":7,"name":"343"},{"age":45,"birthday":1545220726000,"id":8,"name":"455"}]
//criteria.andCondition("age > 50");//手写条件
//[{"age":54,"birthday":1545998303000,"id":5,"name":"deef"}]
//criteria.andGreaterThan("age", 50);//大于
//[{"age":54,"birthday":1545998303000,"id":5,"name":"deef"}]
ArrayList<Integer> arrayList = new ArrayList<>();
arrayList.add(45);
arrayList.add(34);
arrayList.add(36);//数据库无此年龄的人
criteria.andIn("age", arrayList);//多个相等 -------> 相当于 :( age in ( ? , ? , ? ) ) ;
//[{"age":34,"birthday":1545307110000,"id":6,"name":"34"},{"age":34,"birthday":1545307119000,"id":7,"name":"343"},{"age":45,"birthday":1545220726000,"id":8,"name":"455"}]
//criteria.andIsNotNull("name");//不为null
//[{"age":54,"birthday":1545998303000,"id":5,"name":"deef"},{"age":34,"birthday":1545307110000,"id":6,"name":"34"},{"age":34,"birthday":1545307119000,"id":7,"name":"343"},{"age":45,"birthday":1545220726000,"id":8,"name":"455"}]
//criteria.andIsNull("id");//为null
//[]
//criteria.andLessThan("age", "40");//小于
//[{"age":34,"birthday":1545307110000,"id":6,"name":"34"},{"age":34,"birthday":1545307119000,"id":7,"name":"343"}]
//criteria.andLike("name", "%"+"e"+"%");//模糊查询
//[{"age":54,"birthday":1545998303000,"id":5,"name":"deef"}]
//criteria.andNotEqualTo(property, value);//不在这个区间
//criteria.andNotEqualTo(property, value);//不等于
//criteria.andNotIn(property, values);//多个不等于
//criteria.andNotLike(property, value);//模糊查询 非
exa.selectProperties("name","age");//指定要查询的属性列 - 这里会自动映射到表字段
//[{"age":34,"id":0,"name":"34"},{"age":34,"id":0,"name":"343"},{"age":45,"id":0,"name":"455"}]
//exa.setDistinct(false);//去重
//exa.setTableName(tableName);//设置表明
exa.setOrderByClause("create_time desc");//排序
exa.setOrderByClause("age desc");//排序
//[{"age":34,"id":0,"name":"34"},{"age":34,"id":0,"name":"343"},{"age":45,"id":0,"name":"455"}]---->升序
//[{"age":45,"id":0,"name":"455"},{"age":34,"id":0,"name":"34"},{"age":34,"id":0,"name":"343"}]---->降序
List<User> list = userMapper.selectByExample(exa);
System.out.println(JSON.toJSONString(list));
}
}
有什么问题希望在下方留言,大家一起进步…
持续更新…