目录
使用注解方式
1. 创建数据库
create database if not exists my_Sql;
use my_Sql;
create table if not exists mytable
(
ID int primary key not null,
myname varchar(20) unique not null,
myage varchar(20) not null,
power varchar(10) default 'guest' not null
);
insert into mytable(ID, myname, myage, power) value
(1, '张三', '12', 'admin'),
(2, '李四', '12', 'guest'),
(3, '王五', '12', 'guest')
2. 创建项目
引入对应的启动器,选择MySQL和MyBaits依赖
编写一个实体类,实体类的数据需要和数据库数据一一对应
3. 编写配置文件
配置数据源,此处以application.yml为例
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver #连接的数据库
username: root #数据库名称
password: root #数据库密码
url: jdbc:mysql://localhost:3306/my_sql #路径
4. 数据映射(接口)
将数据存入数据库中 , 和从数据库中取数据。
@Mapper
将接口实例化对象,放入Spring容器中,必须注在接口上,在MVC中,这一步实现了M的封装数据
@Select("")
表示注解方法要进行查询操作,和mysql的sql对应,并且查询出的结果,赋给该方法的返回值
5. 实现
6. 地址传参
通过地址栏输入参数,可以显示带有该参数的列表,参数可有多个
@PathVariable
通过 @PathVariable 可以将地址中的参数绑定到方法中,也就是先解析地址中的参数,再赋值给方法里的参数,一般与@RequestMapping( )一起使用。
一个参数:
两个参数:
通过参数删除
@Select("delete from mytable where myname=#{username}")
public List<User> deleteUserAllName(String username);
//地址传参数删除(返回值为列表)
@RequestMapping("/deleteUserAll/{username}")
public List<User> deleteUserAll(@PathVariable("username") String username) {
userMapper.deleteUserAllName(username);
return getUsers();
}
使用配置文件方式
复杂的sql逻辑(一对多,多对多, 多对一)结果集涉及到多个对象 或者按条件查询 插入 修改 或者多 参数的时候
前两步与注解方式相同
3.新建配置文件
配置文件模板
<?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="">
</mapper>
4.编写配置文件
配置数据源,此处以application.yml为例
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: root
url: jdbc:mysql://localhost:3306/my_sql
mybatis:
#配置文件mapper.xml文件所在的位置
mapper-locations: classpath:/mapper/UserMapper.xml
configuration:
#控制台打印SQL
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
# 目的就是让配置文件中的parameterType="UserEntity" resultType="UserEntity"不用再写全包名
type-aliases-package: com.example.mybaits2
5.实现
为了与前端对应连接,所以后端的返回值与前端需要相对应
所以封装一个结果类,以这种形式返回
6.测试接口
7.@RequestBody
一旦前后端进行连接,网络传输不支持传输对象,只能传输字符串,所以用到该注解
作用:在前端将对象转换成字符串后,它将在后端将字符串转换成对象,用于后端传值时
8.if标签
9.where标签
where常常和if标签一起使用,where标签会自动去除and,如果遇到不知道会有几个条件时,可以使用where标签,内部再写if条件
<!-- 多条件查询-->
<select id="getUserByWhile"
parameterType="UserEntity"
resultType="UserEntity">
select * from mytable
<where>
<if test="username!=null and username!=''">
username like '%${username}%'
</if>
<if test="clazz!=null and clazz!=''">
and clazz like '%${clazz}%'
</if>
</where>
</select>
10.sql标签和include标签
重复使用的查询语句可以设为sql
设置sql语句:
<sql id="getUserList"> //此处id可自定义,引用sql时使用
select *
from mytable
</sql>
使用:
<!--查询-->
<select id="getUserList"
resultType="UserEntity">
//include标签引用sql语句
<include refid="getUserList"></include> //refid 引用id,也就是设置的sql的id
</select>
11. foreach 标签
在需要循环操作时,可以在配置文件中写foreach循环,例如:多选删除
12.提交数据
1.post
-----------@RequestBody
axios.post----复杂数据提交,例如添加、修改、删除
前端: 传值
axios.post('/user/getUserByWhile', where).then(res => {
后端: 取值
@RequestMapping("/getUserByWhile")
public ResultMap<List<UserEntity>> getUserByWhile(@RequestBody UserEntity userEntity) {
2.get
------------@RequestMapping("/deleteUserAll/{username}")
前端:
axios.get('/book/getbook', { data: bookID }).then(res => {
后端:
@RequestMapping("/getUsers")
public List<User> getUsers() {
return userMapper.getUsers();
}
13.set标签
在写配置文件时,常常有多个数据需要赋值的情况,这时候无论逗号写在前面还是写在后面,都会出现数据库语句中有额外逗号的情况,这时候set标签,就可以自动添删除不需要的逗号,这里以修改数据为例:
<!--修改-->
<update id="updateUserByID" parameterType="UserEntity">
update mytable
<set> //不用担心查询语句会受逗号影响
<if test="clazz!=null and clazz!=''">
clazz=#{clazz},
</if>
<if test="email!=null and email!=''">
email=#{email},
</if>
<if test="username!=null and username!=''">
username=#{username},
</if>
<if test="password!=null and password!=''">
password=#{password},
</if>
<if test="pID!=null and pID!=''">
pID=#{pID}
</if>
</set>
where userID=#{userID};
</update>