配置的顺序,不配则不用管,配则必须按顺序来!!!!
properties?,
settings?,
typeAliases?,
typeHandlers?,
objectFactory?,
objectWrapperFactory?,
reflectorFactory?,
plugins?,environments?,
databaseIdProvider?,
mappers?.-->
比如我们在引入了jdbc的配置文件使用了properties标签,引入jdbc有什么好处?,可以在配置文件中统一管理
内容而不是在很多个文件改来改去,而且在核心配置文件中把数据库连接相关的写死,显然是硬编码的所以我们用配置文件代替 nice!!!
jdbc.driver=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/mybatis_dbjdbc.username=rootjdbc.password=kobedu#为了解决重名的问题和风险 使用前缀jdbc. 见名示意你可能注意到了上面的写法 用前缀jdbc. 可以很好地将他们与其他的变量区分开,(可以从名字很容易看出是jdbc相关的数据,不至于和同名变量搞混因为username这种可能
不止会出现在数据库的连接)
<properties resource="jdbc.properties"/> <typeAliases> <package name="com.kobedu.com.kobedu.mybatis.pojo"/> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </dataSource> </environment> </environments>上面的代码中引入配置文件的部分为
可以从上面看到写法:
下面这段就是用来设置类的别名:
<typeAliases> <package name="com.kobedu.com.kobedu.mybatis.pojo"/></typeAliases> 那么问题来了,为什么要有类的别名这种操作??
因为在映射文件中每次都要写全类名显然有点麻烦比如下面这样:
<mapper namespace="com.kobedu.com.kobedu.com.kobedu.mybatis.mapper.UserMapper"> <insert id="insertUser"> insert into t_user values (null,"旺财","20") </insert></mapper>一个项目是会有很多个映射文件的为了方便,所以类别名就出现了。可以在核心配置文件写接口类和对应的别名
<typeAliases> <typeAlias type="com.kobedu.com.kobedu.com.kobedu.mybatis.pojo.User" alias="User"/></typeAliases>这样就可以在映射文件的命名空间里可以直接写User(对大小没有要求也可以是user; 其实可以比这更加简单,也是我们在实际开发中常用的写法
就是将整个包写成别名的形式,如果不写alias属性默认为类名(不区分大小写),这样就容易多了,我们只需一行代码,便可以在所有的映射文件命名空间
中直接写对应的类名
<typeAliases> <typeAlias pack="com.kobedu.com.kobedu.mybatis.pojo"/></typeAliases>引入核心的配置文件
<mappers> <mapper resource="mappers/UserMapper.xml"></mapper></mappers>首先需要思考的这里是映射文件的引入,我们正常的一个项目的数据库是有很多个表组成的那么每一张表对应一个mapper接口,每个接口对应一个映射文件,那么就需要导入大量的映射文件,还容易漏掉-->
<mappers> <package name="com.kobedu.com.kobedu.mybatis.mapper"/></mappers>上面这种以包的形式的导入非常方便,不用每次新建一个接口就要导入它的映射文件,但是上面这种写法需要注意一些问题:
如果你在映射文件中编写查询语句的sql,但是粗心的你忘记了设置返回类型会在控制台抛异常且会看到这样的说明:
It's likely that neither a Result Type nor a Result Map was specified.
下面只是指定返回类型的一种方式:resultType,还有 resultMap
它们的区别:
查询的标签必须指定resultType或resultMap
<select id="getUserList" resultType="com.kobedu.com.kobedu.com.kobedu.mybatis.pojo.User"> select * from t_user</select>com.kobedu.mybatis 获取参数的两种方式:${} 和 #{}
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapper PUBLIC "-//com.kobedu.mybatis.org//DTD Mapper 3.0//EN" "http://com.kobedu.mybatis.org/dtd/com.kobedu.mybatis-3-mapper.dtd"><mapper namespace="com.kobedu.com.kobedu.mybatis.mapper.ParameterMapper"> <select id="getUserById" resultType="User"> select * from t_user where userName=#{username} </select></mapper>上面是使用了 #{}写法相当于原生jdbc的占位符,这个前面已经提到过了所以不多赘述,需要注意的是#{}里面的变量名可以是任意的username规范显然很好,但是aaaa也没错因为只是用来占位的;
还有就是在使用${}时注意''单引号问题,因为${}是字符拼接的方式,所以需要注意!!
传输参数时有多个参数时
<select id="getUserByUsernameAndPassword" resultType="User"> select * from t_user where userName=#{username} and password=#{password} </select>在测试代码里通过传入两个参数分别为 username和password 但是在上面代码的(映射文件里的部分代码)执行失败,(sql语句未能解析)
报错:
Cause: org.apache.ibatis.binding.BindingException: Parameter 'username' not found. Available parameters are [arg1, arg0, param1, param2]
可以从错误提示的信息不难发现我们的参数在映射文件里未能真正地接受到,可以用[arg1, arg0, param1, param2] 的方式获取,mybatis将参数放到map容器可以通过建arg0,agr1..的方式
获取参数(也可以是param1,param2..)
将上面的代码改动:
<select id="getUserByUsernameAndPassword" resultType="User"> select * from t_user where userName=#{arg0} and password=#{arg1} </select>需要注意的是:使用${}时需要手动添加''才能正常访问,因为他的处理方式是字符串的拼接
class test{ @Test public void selectUser(){ ParameterMapper parameterMapper = SqlSessionUtils.getSqlSession().getMapper(ParameterMapper.class); User userById = parameterMapper.getUserByUsernameAndPassword("旺财","cwlz"); System.out.println(userById); }}做了改动之后结果很感人!!
User{id=6, userName='旺财', age=20, password='cwlz'}
可以直接通过键访问相对应的值(通过自己的方式访问到数据,上面的形式是mybatis默认提供的map和mybatis默认的提取指的方式 arg0,arg2...)
当需要传多个参数时将他们放到一个map容器,然后将map传给对应的方法(模拟mybatis的做法,就可以在sql语句中直接通过键访问到值)代码如下:
class Test{ @Test public void longinTest(){ ParameterMapper parameterMapper = SqlSessionUtils.getSqlSession().getMapper(ParameterMapper.class); Map<String,Object> userInfo = new HashMap<>(); userInfo.put("username","旺财"); userInfo.put("password","cwlz"); User userById = parameterMapper.login(userInfo); System.out.println(userById); if (userById!=null) { System.out.println("登录成功!"); }else{ System.out.println("用户名或密码错误"); } }}映射文件中的部分代码 :
<select id="login" resultType="User"> select * from t_user where userName=#{username} and password=#{password} </select>通过键直接获取值,注意:使用${}时不要忘了单引号!!!!
当参数以实体对象的形式传参时如何解决?
只需要通过#{}以属性名的方式访问!
class Test{ @Test public void longinTest2(){ ParameterMapper parameterMapper = SqlSessionUtils.getSqlSession().getMapper(ParameterMapper.class); User userById = parameterMapper.loginByUser(new User(null,"旺财",21,'cwlz')); if (userById!=null) { System.out.println("登录成功!"); System.out.println(userById); }else{ System.out.println("用户名或密码错误"); } }}小提醒: 在接受以实体类对象的参数时在sql语句中的属性名一定要和实体类定义时属性的名一样!! 比如在实体类的定义时用户名是这样定义的 private String userName ;但是在映射文件中#{username} 就会报错: Error querying database. Cause: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'username' in 'class com.kobedu.com.kobedu.mybatis.pojo.User'所以代码的编写一定要规范,才能减少这种错误!!!
在声明方法时在参数前用注解 @Param("参数名")
public interface ParameterMapper { //用于测试使用注解的传参方式 User testZhuJie(@Param("z_username") String username, @Param("z_password") String password);}映射文件中的部分代码:(取参数时参数名和方法声明时的注解的中的参数名一一对应)
<select id="testZhuJie" resultType="User"> select * from t_user where userName=#{z_username} and password=#{z_password} </select>小提示:如果映射文件中的参数的名如果和注解中的参数名不一一对应则会报错: Cause: org.apache.ibatis.binding.BindingException: Parameter 'username' not found. Available parameters are [z_username, param1, z_password, param2] 可以从提示看出是参数找不到(参数名写错)!!!一定要和注解中的参数名一一对应!!!
查询功能 1. 若查询出的数据只有一条,可以通过实体类对象接收或者集合类接收 2. 若查询出的数据有多条可以通过List集合接收,一定不能通过实体类对象接收,此时会抛异常TooManyResultsException:(就是预期的结果是一个或者null,但是发现多个就会抛出此类异常)如果查询的结果只有一个,也可以通过Map集合接收,字段名为键字段的值为值:{password=0000, id=3, userName=图区, age=20}
java.lang.Ingeger --> int ,Integer
int --> _int,_Integer
Map --> map
String --> string
[{password=1234, id=1, userName=锁哥, age=21}, {password=3211, id=2, userName=仁王, age=21}, {password=0000, id=3, userName=图区, age=20}, {password=kkkk, id=4, userName=旦罗, age=22}, {password=kobedu, id=5, userName=kobe, age=21}, {password=cwlz, id=6, userName=旺财, age=20}, {password=cwlz, id=9, userName=miss, age=20}, {password=cwlz, id=10, userName=1024, age=20}]@MapKey("id") Map<String,Object> getAllUser();注意:
小提示: 将多条数据用Map接收可以使用@MapKey 将一个字段(在表中独一无二(通常为主键))设为map的键将每一条数据作为值,用Map集合接收! #{} 是自动会添加''的所以在批量删除的案例:需要注意的是不能使用#{} 因为它是会自动添加'' 所以在批量删除的语句中我们要使用${}
<delete id="deleteMore"> delete from t_user where id in (${ids})</delete>select * from ${tableName} 不能使用#{} 会有错用的场景:useGeneratedKeys:设置当前的标签中的sql使用了自增主键keyProperty:将自增主键的值赋值给传输到映射文件中参数的某个属性若字段和属性名不一致,则可以通过resultMap设置自定义映射
在mybatis的核心配置文件用下面的代码将 数据库中命名的规范 (user_name) 转换为 java中的命名规范 (userName)
<settings> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings>就是手动设置属性与字段的映射关系:
如果设置了手动的设置属性和字段的映射关系,注意主键使用 id 标签,普通字段使用 result标签,就算属性和字段名一一对应,只要
用了这种方式就必须要写全!!!
<!--type 映射关系中的实体类--><resultMap id="user_table_map" type="User"><!-- 主键字段--> <id property="id" column="id"></id><!-- 普通字段 property 映射关系中的属性名,column 字段名,当前sql查询出来的字段名--> <result property="userName" column="user_name"></result> <result property="age" column="age"></result> <result property="password" column="password"></result> </resultMap> <select id="getAllUser" resultMap="user_table_map"> select * from t_user </select>表和表之间是有映射的,那么表所对应的实体类之间也是要有映射的,处理多对一的映射关系
一对多的查询:
案例:按照部门的编号查询部门需要返回该部门的员工列表<resultMap id="getDeptAndUserByDidMap" type="Dept"> <id property="id" column="id"></id> <result property="deptName" column="dept_name"></result> <collection property="users" ofType="User"> <id property="id" column="id"></id> <result property="userName" column="user_name"></result> <result property="age" column="age"></result> <result property="password" column="password"></result> </collection> </resultMap> <select id="getDeptAndUserByDid" resultMap="getDeptAndUserByDidMap"> select * from t_dept left join t_user on t_dept.id = t_user.did where t_dept.id = #{did}; </select>通过分步查询实现:
??
多条件的查询
if 根据标签中test的属性所对应的表达式决定标签中的内容是否拼接到sql语句中
<select id="getUserByCondition" resultType="User"> select * from t_user where 'cwlz'='cwlz' <if test="userName != null and userName != '' "> and user_name = #{userName} </if> <if test="age != null and age != '' "> and age = #{age} </if> </select>上面的where后面的 1=1 是细节,因为当where后面的条件都为空时就成了 select * from t_user where
显然这种sql语句是有问题的,还有一种情况就是当userName为null时语句就成了 select * from t_user where and age=#{age}
这也是错的,所以在where后加一个恒成立的条件不仅不会影响查询结果,而且没有会在特定情况时sql语句是会报错的所以很有必要
where 当where标签中有内容时,会自动生成where关键字,并且将内容前多余的and 或者or去掉
当where中没有内容时,此时where标签没有任何效果 就是不会生成关键字注意:在写条件时不能在后面加and or 这个在下一条语句无效时mybatis不会帮你去掉!
<select id="getUserByCondition" resultType="User"> select * from t_user <trim prefix="where" suffix="" prefixOverrides="" suffixOverrides="and|or" > <if test="userName != null and userName != '' "> user_name = #{userName} and </if> <if test="age != null and age != '' "> age = #{age} and </if> <if test="password != null and password !=''"> password = #{password} </if> </trim> </select>相当于 if else
<select id="getUserByCondition" resultType="User"> select * from t_user <where> <choose> <when test="userName != null and userName != '' "> user_name = #{userName} </when> <when test="age != null and age != '' "> age = #{age} </when> <when test="password != null and password != '' "> password = #{password} </when> <otherwise> did = 1 </otherwise> </choose> </where> </select>一个案例 -->就是当我们需要批量删除一些东西时(参数以数组的形式传入)
<delete id="deletesByIds" > delete from t_user where id in ( <foreach collection="ids" item="id" separator=","> #{id} </foreach> ) </delete> <delete id="deletesByIds" > delete from t_user where id in <foreach collection="ids" item="id" separator="or" open="(" close=")"> id = #{id} </foreach> </delete>sql 片段: 在我们的查询语句不能在实际开发中也一直写 *;因为我们要按需查找,不必将不需要的也查询出来,我们可以将我们平常查询次数较多的字段
放在sql片段内,可以在需要查询时直接进行引用!
<sql id="sqldept"> id,dept_name</sql> <select id="getDeptAndById" resultMap="m2"> select <include refid="sqldept"></include> from t_dept where id = #{did} </select>缓存,这个术语我们听过很多次,在web阶段时访问网页时有缓存机制!
现在sql的查询时也有缓存机制,有一级缓存,一级缓存是默认开启的,一级缓存的范围时sqlSession,将我们查询到的数据先进行缓存,若下次有相同的查询时不用重新
访问数据库,可以直接从缓存中取出!!!!
手动清空缓存 sqlSession.clearCache();
一级缓存是默认使用的,而二级缓存需要手动开启!
在mapper配置文件中添加cache标签可以设置一些属性:
一级缓存的作用域是一个sqlsession内;二级缓存的作用域是针对mapper进行缓存<!-- Mybatis EHCache整合包 --><dependency> <groupId>org.mybatis.caches</groupId> <artifactId>mybatis-ehcache</artifactId> <version>1.2.1</version></dependency><!-- slf4j日志门面的一个具体实现 --><dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.3</version></dependency>逆向工程就是不难理解,我们之前都是由实体类到数据库,而逆向类就是通过数据库表生成实体类,
<dependencies> <!-- MyBatis核心依赖包 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.9</version> </dependency> <!-- junit测试 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.2</version> <scope>test</scope> </dependency> <!-- MySQL驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.27</version> </dependency> <!-- log4j日志 --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency></dependencies><!-- 控制Maven在构建过程中相关配置 --><build> <!-- 构建过程中用到的插件 --> <plugins> <!-- 具体插件,逆向工程的操作是以构建过程中插件形式出现的 --> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.0</version> <!-- 插件的依赖 --> <dependencies> <!-- 逆向工程的核心依赖 --> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.2</version> </dependency> <!-- 数据库连接池 --> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.2</version> </dependency> <!-- MySQL驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.27</version> </dependency> </dependencies> </plugin> </plugins></build><?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> <properties resource="jdbc.properties"/> <typeAliases> <package name=""/> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </dataSource> </environment> </environments> <mappers> <package name=""/> </mappers></configuration><?xml version="1.0" encoding="UTF-8"?><!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"><generatorConfiguration> <!-- targetRuntime: 执行生成的逆向工程的版本 MyBatis3Simple: 生成基本的CRUD(清新简洁版) MyBatis3: 生成带条件的CRUD(奢华尊享版) --> <context id="DB2Tables" targetRuntime="MyBatis3"> <!-- 数据库的连接信息 --> <jdbcConnection driver connectionURL="jdbc:mysql://localhost:3306/mybatis_db" userId="root" password="123456"> </jdbcConnection> <!-- javaBean的生成策略--> <javaModelGenerator targetPackage="com.atguigu.mybatis.pojo" targetProject=".\src\main\java"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <!-- SQL映射文件的生成策略 --> <sqlMapGenerator targetPackage="com.atguigu.mybatis.mapper" targetProject=".\src\main\resources"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <!-- Mapper接口的生成策略 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.atguigu.mybatis.mapper" targetProject=".\src\main\java"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <!-- 逆向分析的表 --> <!-- tableName设置为*号,可以对应所有表,此时不写domainObjectName --> <!-- domainObjectName属性指定生成出来的实体类的类名 --> <table tableName="t_emp" domainObjectName="Emp"/> <table tableName="t_dept" domainObjectName="Dept"/> </context></generatorConfiguration> 在maven中找到插件下的generate并执行它你就会发现他自动帮你生成了java的实体类!
逆向工程有两个版本简单版 和 加强版
实际开发中使用的大都是加强版,可以按条件进行增删改查!
<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper --><dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.2.0</version></dependency><plugins> <!--设置分页插件--> <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin></plugins>