上一章我们讲了使用spring boot开一个web服务输出json。现在我们需要连接数据库进行最关键的开发步骤。
上一章 spring boot入门 零基础Java开发入门 spring boot 【系列一】 -- 环境安装和基础实例
Mysql
mysql安装环境看这里 mysql
按上面教程下来,基本不会有问题,不过上面初始化数据库的步骤的时候,记得先切换到mysql得bin目录下面执行,否则最后连接不上。
关于mysql可视化工具自己找,只是玩的话也可以在vscode里面安装可视化插件。
安装好后连接,现在我们新建两个表,一个用户,一个钱包。
Maven
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.1</version>
</dependency>
lombok本来和我们主题关系不大,但是是很有用的一个东西,它可以在定义类的时候不需要写setKEY\getKey这些操作。
安装好依赖后,我们在control旁边新建一个entity目录和mapper目录,作为测试,我们新建一个user的class
package com.example.demo.entity;
import lombok.Data;
@Data
public class User {
private Long id;
private String name;
private Integer age;
}
这里的@Data就是lombok的注解,我们可以免于写冗余的其他代码,但是在引用的时候开发者工具会报错,我们安装lombok的插件就可以。
如图
接下来,我们写一个mapper
public interface UserMapper extends BaseMapper<User> {
}
上面的BaseMapper就是mybatisplus提供的基础类,里面有基本的数据库操作,我们先不进行扩展。
然后,就大功告成了,我们在需要的地方,比如路由里面(控制类)里面使用。
在控制类里面, 写一个查询用户列表的接口,并返回结果。
@RequestMapping(value="/user/list")
public List<User> userList(){
List<User> userList = userMapper.selectList(null);
return userList;
}
然后运行,就可以看到结果啦。看多简单,连xml都不需要写,对于简单的查询删除等,还有比这个更方便的吗。
这里我推荐一个毕竟好的文章(不是官网,官网只是api概述,对新手不友好)字母哥 mybatis 九讲
对于修改也很方便,比如
@RequestMapping(value = "/user/update")
public int update(@RequestParam(value = "name", defaultValue = "") String name, @RequestParam(value = "id", defaultValue = "") Long id ){
User user = new User();
user.setId(id);
user.setName(name);
int rows = userMapper.updateById(user);
return rows;
}
等等,到这里你们会发现一个问题,我数据库都没连接,你怎么查询的。没错,我们还没进行连接。但是这个操作很简单,我们只需要在application.properties添加配置就可以,配置和mybatis的一模一样,所以大家可以看官方文档,我这里列下我的配置
spring.jpa.hibernate.ddl-auto=update
mybatis.type-aliases-package=com.example.demo.dao
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/app?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=test123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
mybatis.config-location=classpath:mybatis/mybatis-config.xml
#mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
mybatis-plus.mapper-locations=classpath:mybatis/mapper/*.xml
这里多了一个mybatis-plus.mapper-locations,这个是可选的,但是我们现在加上,因为,这样可以让我们直接写原生sql。
我们可以在接口上加一个连表查询的接口试试
@RequestMapping(value="/user/walletList")
public List<UserWallet> userWalletList( @RequestParam(value = "id", defaultValue = "") Long id ){
List<UserWallet> userWallet = userMapper.selectUserWallet(id);
return userWallet;
}
先加上控制器,然后加上类和mapper就可以啦。
UserWallet和也放在entity里面
@Data
public class UserWallet {
private Long id;
private String balance;
}
然后关键的mapper来了
public interface UserMapper extends BaseMapper<User> {
List<UserWallet> selectUserWallet(@Param("userId") Long userId);
}
刚刚类里面是空的,现在我们加了一个映射,这里的方法名对应的就是xml里面的id啦。 xml位置就是刚刚在application.properties里面写的mybatis-plus.mapper-locations里面,我们新建一个user.xml
然后写入下面内容
<?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.example.demo.mapper.UserMapper">
<select id="selectUserWallet" resultType="com.example.demo.entity.UserWallet">
SELECT u.id as id, w.balance FROM app.user as u JOIN app.wallet as w on u.id = w.user_id
WHERE u.id = #{userId} LIMIT 100;
</select>
</mapper>
这样就可以完成自定义啦。
运行试下
最后 放下整体的目录结构
下一章 零基础Java开发入门 spring boot 【系列三】跨域、xml转跳、分页等小问题解决
参考
--完--