快速入门
maven构建的springboot项目
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.qj</groupId>
<artifactId>mybatiesPlus</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.22</version>
</dependency>
</dependencies>
</project>
application.yml
spring.datasource.driver-class-name : com.mysql.jdbc.Driver
spring.datasource.url : jdbc:mysql://localhost:3306/bm?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT
spring.datasource.username : root
spring.datasource.password : qianjiang
实体类:pojo
public class User implements Serializable {
private int id;
private String username;
private String password;
private String updateByLast;
private String creatByTime;
}
set和get方法自己补充
然后注意一点:
字段无法映射到对应的实体上,可能是需要用驼峰命名规则
dao层:
public interface IUserMapper extends BaseMapper<User> {
}
springboot启动类:
@SpringBootApplication
@MapperScan("com.qj.dao")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
测试类:
@RunWith(SpringRunner.class)
@SpringBootTest
public class SimpleTest {
@Autowired(required=false)
private IUserMapper iUserMapper;
@Test
public void select(){
System.out.println(iUserMapper.selectList(null).stream().collect(Collectors.toList()));
}
}
查询出来出现
一切正常。这样查询出来的结果为:
项目结构:
下面可以进入学习了
基本使用
以前ssm中,
接口中写抽象方法–>xml或者注解写sql–>Service中调用接口–>Controller中调用service
下面增加日志输出功能,让我们可以看到sql详细日志:
application.yml:
spring.datasource.driver-class-name : com.mysql.jdbc.Driver
spring.datasource.url : jdbc:mysql://localhost:3306/bm?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT
spring.datasource.username : root
spring.datasource.password : qianjiang
mybatis-plus.configuration.log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
新增一名用户:
//增加一名用户
@Test
public void insert(){
Date date = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String d = simpleDateFormat.format(date);
User user = new User();
user.setUsername("皮皮虾");
user.setPassword("30193093");
user.setUpdateByLast(d);
user.setCreatByTime(d);
int i = iUserMapper.insert(user);
System.out.println("影响记录数:"+i);
}
如果表和我的实体类没有对应
注解@TableName()
在实体上面
@TableName("t_user")
public class User(){
}
主键id名和我表中id名不一致:
@Tableld
private int userId;
表的字段名与我类的属性名不一致
@TableField("name")
private String realName;
表中没有字段,我要增加一个怎么操作?
排除 数据库非字段的三种方式(怎么增加一列)
1、
private transient String flag;
表中没有此字段,加上这个注解,解决在表中找不到这个字段名
2、
private static String flag;
public static void setFlag(String flag) {
User.flag = flag;
}
public static String getFlag() {
return flag;
}
表中没有此字段,加上这个注解,解决在表中找不到这个字段名
3、
@TableField(exist = false)
private String flag;
表中没有此字段,加上这个注解,解决在表中找不到这个字段名
查询,通用Mapper
普通查询:
1、根据id来查出一个用户的信息
selectById
==> Preparing: SELECT id,username,password,update_by_last,creat_by_time FROM user WHERE id=?
==> Parameters: 12(Integer)
//根据id来查询
@Test
public void selectById(){
User user = iUserMapper.selectById(12);
System.out.println(user);
}
2、根据多个id查出多个用户的信息
selectBatchIds
==> Preparing: SELECT id,username,password,update_by_last,creat_by_time FROM user WHERE id IN ( ? , ? , ? )
==> Parameters: 12(Integer), 13(Integer), 14(Integer)
//根据多个id来查询多个用户信息
@Test
public void selectByIds(){
List<User> userList =iUserMapper.selectBatchIds(Arrays.asList(12,13,14));
userList.forEach(System.out::println);
}
3、selectByMap()
多个条件查询
返回的是 List
==> Preparing: SELECT id,username,password,update_by_last,creat_by_time FROM user WHERE id = ? AND username = ?
==> Parameters: 12(Integer), xia(String)
@Test
public void selectMap(){
//map.put("name","xia");
//map.put("id",12);
Map<String,Object> columnMap = new HashMap<String,Object>();