如何做网站里的子网站,网页设计与制作第四版,wordpress付费下载功能,淮安建设网站目录
什么是MyBatis#xff1f;
MyBatis入门 1#xff09;创建工程
2#xff09;数据准备
3#xff09;配置数据库连接字符串
4#xff09;写持久层代码
5#xff09;生成测试类
MyBatis打印日志
传递参数
MyBatis的增、删、改
增#xff08;Insert#xff0…目录
什么是MyBatis
MyBatis入门 1创建工程
2数据准备
3配置数据库连接字符串
4写持久层代码
5生成测试类
MyBatis打印日志
传递参数
MyBatis的增、删、改
增Insert
删Delete
改Update
查Select
使用XML方式
增删改查
增Insert
删Delete
改Update
查Select
补充MySQL开发规范 什么是MyBatis
MyBatis是一款持久层框架用于简化JDBC开发 持久层持久化操作的层通常指数据访问层DAO是用来操作数据库的 MyBatis入门
准备工作创建springboot工程、数据库表准备、实体类引入MyBatis的相关依赖配置MyBatis数据库连接信息编写SQL语句注解/XML测试 1创建工程 创建springboot工程并引入MyBatis、MySQL依赖 !--Mybatis 依赖包--
dependencygroupIdorg.mybatis.spring.boot/groupIdartifactIdmybatis-spring-boot-starter/artifactIdversion2.3.1/version
/dependency
!--mysql驱动包--
dependencygroupIdcom.mysql/groupIdartifactIdmysql-connector-j/artifactId
/dependency MyBatis是一个持久层框架具体的数据库存储和数据操作还是在MySQL中操作的 2数据准备 创建用户表
DROP DATABASE IF EXISTS mybatis_test; CREATE DATABASE mybatis_test;
DEFAULT CHARACTER SET utf8mb4;
USE mybatis_test;
DROP TABLE IF EXISTS userinfo;
CREATE TABLE userinfo ( id INT ( 11 ) NOT NULL AUTO_INCREMENT, username VARCHAR ( 127 ) NOT NULL, password VARCHAR ( 127 ) NOT NULL, age TINYINT ( 4 ) NOT NULL, gender TINYINT ( 4 ) DEFAULT 0 COMMENT 1-男 2-⼥ 0-默认, phone VARCHAR ( 15 ) DEFAULT NULL, delete_flag TINYINT ( 4 ) DEFAULT 0 COMMENT 0-正常, 1-删除, create_time DATETIME DEFAULT now(), update_time DATETIME DEFAULT now(), PRIMARY KEY ( id ) ) ENGINE INNODB DEFAULT CHARSET utf8mb4;
INSERT INTO mybatis_test.userinfo ( username, password, age, gender, phone ) VALUES ( admin, admin, 18, 1, 18612340001 ); INSERT INTO mybatis_test.userinfo ( username, password, age, gender, phone ) VALUES ( zhangsan, zhangsan, 18, 1, 18612340002 ); INSERT INTO mybatis_test.userinfo ( username, password, age, gender, phone ) VALUES ( lisi, lisi, 18, 1, 18612340003 ); INSERT INTO mybatis_test.userinfo ( username, password, age, gender, phone ) VALUES ( wangwu, wangwu, 18, 1, 18612340004 ); 创建对应实体类UserInfo
注意实体类中的属性名与表中的字段名一一对应
import lombok.Data;
import java.util.Date;
Data
public class UserInfo {private Integer id;private String username;private String password;private Integer age;private Integer gender;private String phone;private Integer deleteFlag;private Date createTime;private Date updateTime;
} 3配置数据库连接字符串
spring:datasource:url: jdbc:mysql://127.0.0.1:3306/mybatis_test?characterEncodingutf8useSSLfalseusername: rootpassword: root #改为你的数据库密码driver-class-name: com.mysql.cj.jdbc.Driver 4写持久层代码
创建持久层接口UserInfoMapper Mapper
public interface UserInfoMapper {Select(select username,password,age,gender,phone from userinfo)public ListUserInfo queryAllUser();
}Mybatis的持久层接⼝规范⼀般都叫XxxMapper Mapper注解表⽰是MyBatis中的Mapper接⼝ 程序运⾏时,框架会⾃动⽣成接⼝的实现类对象(代理对象)并给交Spring的IOC容器管理 5生成测试类
在需要测试的Mapper接口中右键-Generate-Test
书写测试代码
SpringBootTest
class UserInfoMapperTest {Autowiredprivate UserInfoMapper userInfoMapper;Testvoid queryAllUser() {ListUserInfo userInfoListuserInfoMapper.queryAllUser();System.out.println(userInfoList);}
}
注解SpringBootTest该测试类在运行时就会自动加载Spring的运行环境 MyBatis打印日志
通过日志看到sql语句的执行、执行传递的参数以及执行结果
mybatis:configuration: # 配置打印 MyBatis⽇志log-impl: org.apache.ibatis.logging.stdout.StdOutImpl ①查询语句 ②传递参数及类型 ③SQL执行结果 传递参数
Mapper
public interface UserInfoMapper {Select(select username,password,age,gender,phone from userinfo where id#{id})public ListUserInfo queryAllUser(Integer id);
}
SpringBootTest
class UserInfoMapperTest {Autowiredprivate UserInfoMapper userInfoMapper;Testvoid queryAllUser() {ListUserInfo userInfoListuserInfoMapper.queryAllUser(4);System.out.println(userInfoList);}
} MyBatis的增、删、改
增Insert
Mapper中
Mapper
public interface UserInfoMapper {Options(useGeneratedKeys true,keyProperty id)Insert(insert into userinfo(username,password,age,gender,phone) values (#{username},#{password},#{age},#{gender},#{phone}))public Integer insert(UserInfo userInfo);}
MapperTest中
SpringBootTest
class UserInfoMapperTest {Testvoid insert() {UserInfo userInfonew UserInfo();userInfo.setAge(10);userInfo.setGender(2);userInfo.setUsername(赵六);userInfo.setPhone(123456);userInfo.setPassword(123);userInfoMapper.insert(userInfo);}
} 注意如果使用了Param属性来重命名#{...}需要使用“参数.属性”来获取 Insert(insert into userinfo(username,password,age,gender,phone) values (#{userinfo.username},#{userinfo.password},#{userinfo.age},#{userinfo.gender},#{userinfo.phone}))
public Integer insert(Param(userinfo) UserInfo userInfo);
Insert语句返回值是收影响的行数
有些情况下我们需要获得新插入数据的id此时使用Options注解
Options(useGeneratedKeys true,keyProperty id)Insert(insert into userinfo(username,password,age,gender,phone) values (#{username},#{password},#{age},#{gender},#{phone}))public Integer insert(UserInfo userInfo);Test
void insert() {UserInfo userInfonew UserInfo()userInfo.setAge(10);userInfo.setGender(2);userInfo.setUsername(赵六);userInfo.setPhone(123456);userInfo.setPassword(123);Integer count userInfoMapper.insert(userInfo);//设置useGeneratedKeystrue后方法返回值仍然是受影响行数System.out.println(添加数据条数: count 数据id:userInfo.getId());
} useGeneratedKeys令MyBatis使⽤JDBC的getGeneratedKeys⽅法来取出由数据库内部⽣成的主键默认值false. keyProperty这指定了实体类中用于存储数据库生成的主键的属性的名称当 MyBatis 从数据库获取到新生成的主键后它会将这个值设置到实体类的 id 属性中。 删Delete
Delete(delete from userinfo where id#{id})public void delete(Integer id);
Test
void delete() {userInfoMapper.delete(4);
} 改Update
Update(update userinfo set username#{username} where id#{id})
public void update(String username,Integer id);
Test
void update() {userInfoMapper.update(王五,3);
} 查Select
上述查询中我们发现只有在java对象属性和数据库字段名一样忽略大小写时才会进行赋值
java对象中deleteFlag、createTime、updateTime属性与数据库中字段delete_flag、create_time、update_time对应不上 解决办法
①起别名
Select(select id, username, password, age, gender, phone, delete_flag as
deleteFlag,create_time as createTime, update_time as updateTime from userinfo)
public ListUserInfo queryAllUser();②结果映射
Select(select id, username, password, age, gender, phone, delete_flag,
create_time, update_time from userinfo)
Results({Result(column delete_flag,property deleteFlag),Result(column create_time,property createTime),Result(column update_time,property updateTime)
})
ListUserInfo queryAllUser();可以给Results定义一个名称使其他sql也能复用这个映射关系
Select(select id, username, password, age, gender, phone, delete_flag,
create_time, update_time from userinfo)
Results(id resultMap,value {Result(column delete_flag,property deleteFlag),Result(column create_time,property createTime),Result(column update_time,property updateTime)
})
ListUserInfo queryAllUser();
Select(select id, username, password, age, gender, phone, delete_flag,
create_time, update_time from userinfo where id #{userid} )
ResultMap(value resultMap)
UserInfo queryById(Param(userid) Integer id);③开启驼峰命名推荐
数据库中字段通常使用蛇形命名java属性通常使用驼峰命名可以通过配置使得这两种命名方式自动映射abc_xyz abcXyz
mybatis:configuration:map-underscore-to-camel-case: true #配置驼峰⾃动转换 MyBatis有使用注解和XML两种方式
下面我们介绍
使用XML方式
①配置数据库连接字符串
#数据库连接配置
spring:datasource:url: jdbc:mysql://127.0.0.1:3306/mybatis_test?characterEncodingutf8useSSLfalseusername: rootpassword: root //改成你自己的密码driver-class-name: com.mysql.cj.jdbc.Driver
#配置 mybatis xml 的⽂件路径在 resources/mapper 创建所有表的 xml ⽂件
mybatis:mapper-locations: classpath:mapper/**Mapper.xml ②写持久层代码
1方法定义Interface
Mapper
public interface UserInfoXMLMapper {ListUserInfo queryAllUser();
}
2方法实现UserInfoXMLMapper.xml路径参考yml中的配置 mapper-locations: classpath:mapper/**Mapper.xml ?xml version1.0 encodingUTF-8?
!DOCTYPE mapper PUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtd
mapper namespacecom.example.sql.mapper.UserInfoMapperselect idqueryAllUser resultTypecom.example.sql.UserInfoselect username,password,age,gender,phone from userinfo/select
/mappermapper标签需要指定namespace 属性表⽰命名空间值为mapper接⼝的全限定 名包括全包名.类名。select查询标签是⽤来执⾏数据库的查询操作的 ◦id 是和Interface 接⼝中定义的⽅法名称⼀样的表⽰对接⼝的具体实现⽅法。 ◦ resultType 是返回的数据类型也就是开头我们定义的实体类 单元测试
SpringBootTest
public class UserInfoXMLMapperTest {Autowiredprivate UserInfoXMLMapper userInfoXMLMapper;Testvoid queryAllUser(){ListUserInfo userInfoListuserInfoXMLMapper.queryAllUser();System.out.println(userInfoList);}} 增删改查
增Insert
Mapper
public interface UserInfoXMLMapper {Integer insertUser(UserInfo userInfo);
}
?xml version1.0 encodingUTF-8?
!DOCTYPE mapper PUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtd
mapper namespacecom.example.sql.mapper.UserInfoXMLMapperinsert idinsertUser useGeneratedKeystrue keyPropertyidinsert into userinfo (username,password,age,gender,phone) values(#{username},#{password},#{age},#{gender},#{phone})/insert
/mapperSpringBootTest
public class UserInfoXMLMapperTest {Autowiredprivate UserInfoXMLMapper userInfoXMLMapper;Testvoid insertUser(){UserInfo userInfonew UserInfo();userInfo.setAge(10);userInfo.setGender(2);userInfo.setUsername(赵七);userInfo.setPhone(123457);userInfo.setPassword(123);Integer countuserInfoXMLMapper.insertUser(userInfo);System.out.println(count);}
} 使用Param设置参数名称与注解类似
Integer insertUser(Param(userinfo) UserInfo userInfo);insert idinsertUserinsert into userinfo (username, password, age, gender, phone) values(#{userinfo.username},#{userinfo.password},#{userinfo.age},#
{userinfo.gender},#{userinfo.phone})
/insert返回自增id
insert idinsertUser useGeneratedKeystrue keyPropertyidinsert into userinfo (username, password, age, gender, phone) values(#{userinfo.username},#{userinfo.password},#{userinfo.age},#
{userinfo.gender},#{userinfo.phone})
/insert删Delete
UserInfoXMLMapper接⼝
Integer deleteUser(Integer id);
UserInfoXMLMapper.xml实现:
delete iddeleteUserdelete from userinfo where id #{id}
/delete改Update
UserInfoXMLMapper接⼝:
Integer updateUser(UserInfo userInfo);
UserInfoXMLMapper.xml实现: update idupdateUser update userinfo set username#{username} where id#{id} update 查Select
与注解方式类似查找操作也涉及到java对象和数据库字段命名问题
解决办法
①起别名
②结果映射
③开启驼峰命名
①③与注解一样下面介绍结果映射
resultMap idBaseMap typecom.example.demo.model.UserInfoid columnid propertyid/idresult columndelete_flag propertydeleteFlag/resultresult columncreate_time propertycreateTime/resultresult columnupdate_time propertyupdateTime/result
/resultMap
select idqueryAllUser resultMapBaseMapselect id, username,password, age, gender, phone, delete_flag,
create_time, update_time from userinfo
/select开发中建议简单sql使用注解方式复杂sql如动态sql使用xml方式
注解方式和xml方式可以一起使用 补充MySQL开发规范
①表名、字段名使⽤⼩写字⺟或数字,单词之间以下划线分割.尽量避免出现数字开头或者两个下划线中间只出现数字.数据库字段名的修改代价很⼤,所以字段名称需要慎重考虑。 MySQL在Windows下不区分⼤⼩写,但在Linux下默认是区分⼤⼩写. 因此,数据库名、表名、字段名都不允许出现任何⼤写字⺟,避免节外⽣枝 ②表必备三字段:id,create_time,update_timeid必为主键,类型为bigintunsigned,create_time,update_time的类型均为datetime类型,create_time表⽰创建时间, update_time表⽰更新时间
③在表查询中,避免使⽤*作为查询的字段列表,标明需要哪些字段 1. 增加查询分析器解析成本 2. 增减字段容易与resultMap配置不⼀致 3. ⽆⽤字段增加⽹络消耗,尤其是text类型的字段