1.引入第三方工具mybatis的场景启动器
y
如果有错误可以根据Ide提示修改
2.在resources下建立mybatis文件写入mybatis-config.xml全局配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="mapUnderscoreToCameCase" value="ture"/> //开启驼峰命名规则
</settings>
</configuration>
再在mybatis下建立mapper文件夹,放入AccountMapper.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.ml.admin.mapper.AccountMapper">
<select id="getAccount_id" resultType="com.ml.admin.bean.Account">
select * from student where id = #{id}
</select>
</mapper>
另一个项目中mapper.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.music.mapper.ListSongMapper">
<resultMap id="BaseSelect" type="com.example.music.entity.ListSong">
<id column="id" jdbcType="INTEGER" property="id"/>
<result column="song_id" jdbcType="INTEGER" property="songId"/>
<result column="song_list_id" jdbcType="INTEGER" property="songListId"/>
</resultMap>
<!--增加歌单歌曲-->
<insert id="insertListSong" parameterType="com.example.music.entity.ListSong">
insert into list_song
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="songId!=null">song_id,</if>
<if test="songListId!=null">song_list_id</if>
</trim>
<trim prefix="values(" suffix=")" suffixOverrides=",">
<if test="songId!=null">#{songId},</if>
<if test="songListId!=null">#{song_list_id}</if>
</trim>
</insert>
<!--删除歌单歌曲-->
<delete id="deleteListSong" parameterType="java.lang.Integer">
delete
from list_song
where id = #{id}
</delete>
<!--修改歌单歌曲-->
<update id="updateListSong" parameterType="com.example.music.entity.ListSong">
update list_song
<set>
<if test="songId!=null">song_id=#{songId}</if>
<if test="songListId!=null">song_list_id#{songListId}</if>
</set>
where id = #{id}
</update>
<!--根据主键id查询歌单歌曲-->
<select id="selectById" parameterType="java.lang.Integer" resultMap="BaseSelect">
select *
from list_song
where id = # {id}
</select>
<!--查询所有的歌曲歌单-->
<select id="selectAll" resultMap="BaseSelect">
select *
from list_song
</select>
<!--根据歌单id查询所有的歌曲-->
<select id="selectBySongListId" resultMap="BaseSelect">
select *
from list_song
where song_list_id = #{songListId}
</select>
</mapper>
3.在bean下写一个Acount类封装student表中的信息,如下
————————————————————————————————————————————————————————————
package com.ml.admin.bean;
import lombok.Data;
@Data
public class Account {
private Integer id;
private String name;
private Integer age;
}
————————————————————————————————————————————————————————————
4.在mapper文件下写一个AccountMapper接口操作数据表student表,如下
@Mapper
public interface AccountMapper {
public Account getAccount_id(Integer id);
}
————————————————————————————————————————————————————————————
5.在application.yaml中配置mybatis映射规则
spring:
datasource:
username: root
password: 123456
#假如时区报错了,就增加一个时区配置即可
url: jdbc:mysql://localhost:3306/product?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8
driver-class-name: com.mysql.cj.jdbc.Driver
# 指定数据源的类型,spring默认的数据源是hikari,指定我们自己引入的德鲁伊数据源
type: com.alibaba.druid.pool.DruidDataSource
filters: stat,wall,log4j
jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: GMT+8
servlet:
multipart:
enabled: true
max-file-size: 2000MB
max-request-size: 2000MB
mybatis:
mapper-locations: classpath:/mybatis/mapper/*.xml
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
server:
port: 8888
mybatis:
config-location: classpath:mybatis/mybatis-config.xml #全局配置文件路径
mapper-locations: classpath:mybatis/mapper/*.xml #sql映射文件路径
6.在service下创建AccountService.java文件用于实现AccountMapper接口,如下
————————————————————————————————————————————————————————————
package com.ml.admin.service;
import com.ml.admin.bean.Account;
import com.ml.admin.mapper.AccountMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class AccountService {
@Autowired
AccountMapper accountMapper;
public Account get_by_id(Integer id) {
return accountMapper.getAccount_id(id);
}
}
————————————————————————————————————————————————————————————
7.使用@ResponseBody返回查询数据,代码如下
————————————————————————————————————————————————————————————
@Autowired
AccountService accountService;
@ResponseBody //用于返回json数据,要是没有这个页面就会解析错误
@GetMapping(“/account”)
public Account get_id(@RequestParam(“id”) Integer id){
return accountService.get_by_id(id);
}
————————————————————————————————————————————————————————————
student表
结果如下
注意
mybatis:
#config-location: classpath:mybatis/mybatis-config.xml #全局配置文件路径
mapper-locations: classpath:mybatis/mapper/*.xml #sql映射文件路径
configuration: # 和上面的全局配置文件只能存在一个,所以可以删除mybatis-config.xml文件
map-underscore-to-camel-case: true
依旧访问正常