马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
x
目录
1. 配置yml配置文件
1.2 配置数据库
1.3 配置xml的路径
2. xml文件中实现数据库的增删查改操纵
2.1 各文件内容
2.2 编写细节
MyBatis作为一个持久层框架,用于进行数据库操纵。
MyBatis的实现方式有两种:(1)注解;(2)XML;
本文先容基于XML实现MyBatis。
1. 配置yml配置文件
1.2 配置数据库
- spring:
- datasource:
- url: jdbc:mysql://127.0.0.1:3306/mybatis_test?characterEncoding=utf8&useSSL=false
- username: root
- password: xxxxxx
- driver-class-name: com.mysql.cj.jdbc.Driver
复制代码 1.3 配置xml的路径
- mybatis:
- configuration:
- log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
- map-underscore-to-camel-case: true
- mapper-locations: classpath:mapper/**Mapper.xml
复制代码 此中:
mapper是resources目录下的子目录名,**Mapper.xml表现该XML文件的定名方式必须以Mapper.xml结尾,二者均可自定名,注意对应即可;
注:注意mapper-locations与configurations同层,都属于mybatis的下一层,注意对准层次;
2. 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.zhouyou.mybatisdemo1.mapper.UserInfoXMLMapper">
- </mapper>
复制代码 此中,namespace的值是待实现接口(UserInfoXMLMapper)的全限定类名;
现使用MyBatis操纵数据库实现增删查改操纵。
创建一个UserInfoXMLMapper接口,用于编写方法声明;
在resource下创建一个mapper包,再创建UserInfoXMLMapper.xml用于进行数据持久层的实现;
目录结构如下:
2.1 各文件内容
1、在Mapper类中编写各个方法声明:
- package com.zhouyou.mybatisdemo1.mapper;
- import com.zhouyou.mybatisdemo1.model.UserInfo;
- import org.apache.ibatis.annotations.Mapper;
- import org.apache.ibatis.annotations.Param;
- import java.util.List;
- @Mapper
- public interface UserInfoXMLMapper {
- List<UserInfo> selectAll();
- Integer insert(@Param("userInfo") UserInfo userInfo);
- Integer delete(Integer id);
- Integer update(UserInfo userInfo);
- }
复制代码 2、 在UserInfoXMLMapper.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.zhouyou.mybatisdemo1.mapper.UserInfoXMLMapper">
- <select id="selectAll" resultType="com.zhouyou.mybatisdemo1.model.UserInfo" >
- select* from userInfo
- </select>
- <insert id="insert" 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 id="delete">
- delete from userinfo where id=#{id}
- </delete>
- <update id="update">
- update userinfo set age=#{age} where id=#{id}
- </update>
- </mapper>
复制代码 3、创建测试类UserInfoXMLMapperTest内容如下:
- package com.zhouyou.mybatisdemo1.mapper;
- import com.zhouyou.mybatisdemo1.model.UserInfo;
- import lombok.extern.slf4j.Slf4j;
- import org.junit.jupiter.api.Test;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.test.context.SpringBootTest;
- import java.util.List;
- import static org.junit.jupiter.api.Assertions.*;
- @Slf4j
- @SpringBootTest
- class UserInfoXMLMapperTest {
- @Autowired
- private UserInfoXMLMapper userInfoXMLMapper;
- @Test
- void selectAll() {
- List<UserInfo> userInfos=userInfoXMLMapper.selectAll();
- log.info(userInfos.toString());
- }
- @Test
- void delete() {
- userInfoXMLMapper.delete(10);
- }
- @Test
- void update() {
- UserInfo userInfo=new UserInfo();
- userInfo.setAge(21);
- userInfo.setId(8);
- userInfoXMLMapper.update(userInfo);
- }
- @Test
- void insert() {
- UserInfo userInfo=new UserInfo();
- userInfo.setUsername("tianqi");
- userInfo.setPassword("tianqi");
- userInfo.setAge(20);
- userInfo.setGender(2);
- userInfo.setPhone("18612340006");
- Integer result = userInfoXMLMapper.insert(userInfo);
- log.info("affected rows: {}\n"+
- "auto_increment primary key: {}",result,userInfo.getId());
- }
- }
复制代码 以上四个方法分别实现:全列选择查询、新增、更新/更改、删除;
2.2 编写细节
1、关于<select>标签:
select标签的id指明方法,注意需与接口定义的方法名保持同等,否则会报绑定错误:
select标签的resultType指明返回的数据的范例;
2、关于参数重定名问题,使用xml实现MyBatis和使用注解实现MyBatis方法相同,使用@Param注解实现。并且当参数范例为对象时,若进行了重定名,则需使用 对象名.属性名 作为参数名;
3、对selectAll(或selectOne)、insert、update、delete四个方法,只有selectAll(或selectOne)的<select>标签需设置resultType,其余三个方法无需设置;
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |