文章目录
- 🎍配置数据库连接和MyBatis
- 🍀写持久层代码
- 🌸添加mapper接口
- 🌸添加UserInfoXMLMapper.xml
- 🌸单元测试
- 🌲CRUD
- 🌸增(Insert)
- 🌸删(Delete)
- 🌸改(Update)
- 🌸查(Select)
MyBatis XML的⽅式需要以下两步:
-
配置数据库连接字符串和MyBatis
-
写持久层代码
🎍配置数据库连接和MyBatis
application.yml⽂件配置内容如下:
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/mybatis_test?
characterEncoding=utf8&useSSL=false
username: root
password: 你自己设置的密码
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
configuration: # 配置打印 MyBatis⽇志
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
mapper-locations: classpath:mapper/**Mapper.xml
如果是application.properties⽂件,配置内容如下:
#驱动类名称
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#数据库连接的url
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/mybatis_test?
characterEncoding=utf8&useSSL=false
#连接数据库的⽤⼾名
spring.datasource.username=root
#连接数据库的密码
spring.datasource.password=root
# 配置 mybatis xml 的⽂件路径,在 resources/mapper 创建所有表的 xml ⽂件
mybatis.mapper-locations=classpath:mapper/**Mapper.xml
🍀写持久层代码
持久层代码分两部分
- ⽅法定义:Interface
- ⽅法实现:XXX.xml
🌸添加mapper接口
数据持久层的接⼝定义:
@Mapper
public interface UserInfoXMlMapper {
}
🌸添加UserInfoXMLMapper.xml
数据持久成的实现,MyBatis的固定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="org.example.mybatis.mapper.UserInfoXMlMapper">
</mapper>
创建UserInfoXMLMapper.xml,路径参考yml中的配置
查询所有⽤⼾的具体实现:
<?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.UserInfoXMlMapper">
<select id="queryAllUser" resultType="com.example.demo.model.UserInfo">
select username,`password`, age, gender, phone from userinfo
</select>
</mapper>
以下是对以上标签的说明:
• 标签:需要指定namespace 属性,表⽰命名空间,值为mapper接⼝的全限定
名,包括全包名.类名。
• 查询标签:是⽤来执⾏数据库的查询操作的:
◦ id :是和Interface (接⼝)中定义的⽅法名称⼀样的,表⽰对接⼝的具体实现⽅法。
◦ resultType :是返回的数据类型,也就是开头我们定义的实体类.
🌸单元测试
@SpringBootTest
class UserInfoMapperTest {
@Autowired
private UserInfoMapper userInfoMapper;
@Test
void queryAllUser() {
List<UserInfo> userInfoList = userInfoMapper.queryAllUser();
System.out.println(userInfoList);
}
}
🌲CRUD
🌸增(Insert)
UserInfoMapper接⼝:
Integer insertUser(UserInfo userInfo);
UserInfoMapper.xml实现:
<insert id="insertUser">
insert into userinfo (username, `password`, age, gender, phone) values (#
{username}, #{password}, #{age},#{gender},#{phone})
</insert>
如果使⽤@Param设置参数名称的话,使⽤⽅法和注解类似
UserInfoMapper接⼝:
Integer insertUser(@Param("userinfo") UserInfo userInfo);
UserInfoMapper.xml实现:
<insert id="insertUser">
insert into userinfo (username, `password`, age, gender, phone) values
(#{userinfo.username},#{userinfo.password},#{userinfo.age},#
{userinfo.gender},#{userinfo.phone})
</insert>
返回⾃增id
接⼝定义不变,Mapper.xml实现设置useGeneratedKeys和keyProperty属性
<insert id="insertUser" useGeneratedKeys="true" keyProperty="id">
insert into userinfo (username, `password`, age, gender, phone) values
(#{userinfo.username},#{userinfo.password},#{userinfo.age},#
{userinfo.gender},#{userinfo.phone})
</insert>
🌸删(Delete)
UserInfoMapper接⼝:
Integer deleteUser(Integer id);
UserInfoMapper.xml实现:
<delete id="deleteUser">
delete from userinfo where id = #{id}
</delete>
🌸改(Update)
UserInfoMapper接⼝:
Integer updateUser(UserInfo userInfo);
UserInfoMapper.xml实现:
<update id="updateUser">
update userinfo set username=#{username} where id=#{id}
</update>
🌸查(Select)
同样的,使⽤XML的⽅式进⾏查询,也存在数据封装的问题
我们把SQL语句进⾏简单修改,查询更多的字段内容
<select id="queryAllUser" resultType="com.example.demo.model.UserInfo">
select id, username,`password`, age, gender, phone, delete_flag,
create_time, update_time from userinfo
</select>
结果显⽰:deleteFlag,createTime,updateTime也没有进⾏赋值.
解决办法和注解类似:
- 起别名
- 结果映射
- 开启驼峰命名
其中1,3的解决办法和注解⼀样,不再多说,接下来看下xml如果来写结果映射
Mapper.xml
<resultMap id="BaseMap" type="com.example.demo.model.UserInfo">
<id column="id" property="id"></id>
<result column="delete_flag" property="deleteFlag"></result>
<result column="create_time" property="createTime"></result>
<result column="update_time" property="updateTime"></result>
</resultMap>
<select id="queryAllUser" resultMap="BaseMap">
select id, username,`password`, age, gender, phone, delete_flag,
create_time, update_time from userinfo
</select>
⭕总结
感谢大家的阅读,希望得到大家的批评指正,和大家一起进步,与君共勉!