Mybatis
一、配置
1.1 配置文件
- 生成配置文件:
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>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="org/mybatis/example/BlogMapper.xml"/>
</mappers>
</configuration>
- <transactionManager 事务管理
1.2 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.eat.dao.mapper.ClientMapper">
<resultMap id="BaseResultMap" type="com.example.eat.dao.pojo.Client">
<id column="user_id" jdbcType="INTEGER" property="userId" />
<result column="username" jdbcType="VARCHAR" property="username" />
<result column="password" jdbcType="VARCHAR" property="password" />
<result column="phonenumber" jdbcType="VARCHAR" property="phonenumber" />
<result column="head_portrait" jdbcType="VARCHAR" property="headPortrait" />
<result column="reputation" jdbcType="VARCHAR" property="reputation" />
</resultMap>
<select id="selectByUserId" parameterType="com.example.eat.dao.pojo.Grope" resultMap="GroupResultMap">
SELECT * FROM food join menu on food.menu_id = menu.menu_id where user_id = #{userId,jdbcType=INTEGER}
</select>
- mapper接口的路径
- result:
- resultType:返回单条
- resultMap: 返回集合
-
select:
-
id: 接口方法名
-
resultType:sql语句执行的返回值
-
parameterType: 参数类型
-
SELECT * FROM food join menu on food.menu_id = menu.menu_id where user_id = #{userId,jdbcType=INTEGER}
-
-
-
insert:
- 没有返回值
1.3 万能Map
map当参数
1.4 模糊查询
参数传递通配符%李%
1.5 generator
<?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>
<!--数据库驱动地址-->
<classPathEntry
location="G:\working\Maven\localRepo\mysql\mysql-connector-java\8.0.27\mysql-connector-java-8.0.27.jar"/>
<context id="DB2Tables" targetRuntime="Mybatis3">
<commentGenerator>
<property name="suppressData" value="true"/>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--驱动、数据库、账号‘密码-->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/eat-what"
userId="root"
password="haorui1996">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!--生成的包名、要生成包所在的位置-->
<!--模型-->
<javaModelGenerator targetPackage="pojo" targetProject="src/main/java/com/example/eat/dao"/>
<!--映射-->
<sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources"/>
<!--dao类位置-->
<javaClientGenerator type="XMLMAPPER" targetPackage="mapper" targetProject="src/main/java/com/example/eat/dao"/>
<!--生成的表及其对应的类名-->
<table tableName="client" domainObjectName="Client"
enableCountByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
enableUpdateByExample="false"
selectByExampleQueryId="false"/>
<table tableName="menu" domainObjectName="Menu"
enableCountByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
enableUpdateByExample="false"
selectByExampleQueryId="false"/>
<table tableName="food" domainObjectName="Food"
enableCountByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
enableUpdateByExample="false"
selectByExampleQueryId="false"/>
</context>
</generatorConfiguration>
二、配置优化
2.1 配置环境
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-8ropkrm8-1652711597965)(C:\Users\70402\AppData\Roaming\Typora\typora-user-images\image-20220117232058918.png)]
三、动态SQL
-
if
//查询user表,如果查询title不为空则按照给定title查询,否则查询所有 <select id="queryBlogIF" parameterType="map" resultType="blog"> select * form user where 1=1 <if test="title !=null"> title = #{title} </select>
-
where
//sql语句where后面不能直接跟and 所以使用where标签,当匹配到第二个时自动删除and <select id="selectIf" parameterType="map" resultType="map"> select * from customer <where> <if test="id != null"> id = #{id} </if> <if test="author != null"> and author = #{author} </if> </where> </select>