一、pom.xml
主要是用来引入jar包依赖,以下是几种常用的jar包依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.lagou</groupId>
<artifactId>MyBatisLagouWork</artifactId>
<version>1.0-SNAPSHOT</version>
<!--指定编码及版本-->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
<java.version>1.11</java.version>
<maven.compiler.source>1.11</maven.compiler.source>
<maven.compiler.target>1.11</maven.compiler.target>
</properties>
<dependencies>
<!--mybatis坐标-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.4</version>
</dependency>
<!--mysql驱动坐标-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
<scope>runtime</scope>
</dependency>
<!--单元测试坐标-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!--日志-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
</project>
二、核心配置文件
通常命名为:SqlMapConfig.xml主要是加载properties文件、环境配置以及映射配置,还有些其他配置(如缓存配置)
<!--核心配置文件的头部信息-->
<?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文件-->
<properties resource="jdbc.properties"></properties>
<!--设置别名-->
<typeAliases>
<!--方式一:给单个实体起别名-->
<!-- <typeAlias type="com.lagou.domain.User" alias="user"></typeAlias>-->
<!--方式二:批量起别名 别名就是类名,且不区分大小写-->
<package name="com.lagou.domain"/>
</typeAliases>
<!--分页助手-->
<plugins>
<plugin interceptor="com.github.pagehelper.PageHelper">
<!--dialect: 指定方言 limit-->
<property name="dialect" value="mysql"/>
</plugin>
</plugins>
<!--environments: 运行环境-->
<environments default="development">
<environment id="development">
<!--当前的事务事务管理器是JDBC-->
<transactionManager type="JDBC"></transactionManager>
<!--数据源信息 POOLED:使用mybatis的连接池-->
<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>
<!--<mapper resource="com/lagou/mapper/UserMapper.xml"></mapper>-->
<!--使用该方式:接口和映射文件需要同包同名-->
<!-- <mapper class="com.lagou.mapper.UserMapper"></mapper>-->
<!--批量加载映射-->
<package name="com.lagou.mapper"/>
</mappers>
</configuration>
三、映射配置文件
主要是编写sql代码映射文件的概述: 示例:
<?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.lagou.mapper.UserMapper">
<!--根据id查询用户-->
<select id="findUserById" parameterType="int" resultMap="userResultMap">
select * from user where id = #{id}
</select>
<!--id : 标签的唯一标识
type: 封装后实体类型-->
<resultMap id="userResultMap" type="com.lagou.domain.User">
<!--手动配置映射关系-->
<!--id: 用来配置主键-->
<id property="id" column="id"></id>
<!-- result: 表中普通字段的封装-->
<result property="username" column="username"></result>
<result property="birthday" column="birthday"></result>
<result property="sex" column="sex"></result>
<result property="address" column="address"></result>
</resultMap>
<!--查询所有用户-->
<!--resultMap:手动配置实体属性与表中字段的映射关系,完成手动封装-->
<select id="findAllResultMap" resultMap="userResultMap">
select * from user
</select>
<!--多条件查询:方式一-->
<select id="findByIdAndUsername1" resultMap="userResultMap" >
<!-- select * from user where id = #{arg0} and username = #{arg1}-->
select * from user where id = #{param1} and username = #{param2}
</select>
<!--多条件查询:方式二-->
<select id="findByIdAndUsername2" resultMap="userResultMap" >
select * from user where id = #{id} and username = #{username}
</select>
<!--多条件查询:方式三-->
<select id="findByIdAndUsername3" resultMap="userResultMap" parameterType="com.lagou.domain.User">
select * from user where id = #{id} and username = #{usernameabc}
</select>
<!--模糊查询:方式一-->
<select id="findByUsername" resultMap="userResultMap" parameterType="string">
<!-- #{}在mybatis中是占位符,引用参数值的时候会自动添加单引号 -->
select * from user where username like #{username}
</select>
<!--模糊查询:方式二-->
<select id="findByUsername2" resultMap="userResultMap" parameterType="string">
<!--parameterType是基本数据类型或者String的时候,${}里面的值只能写value
${}: sql原样拼接
-->
select * from user where username like '${value}'
</select>
<!--添加用户:获取返回主键:方式一-->
<!--
useGeneratedKeys: 声明返回主键
keyProperty:把返回主键的值,封装到实体中的那个属性上
-->
<insert id="saveUser" parameterType="user" useGeneratedKeys="true" keyProperty="id">
insert into user(username,birthday,sex,address) values(#{username},#{birthday},#{sex},#{address})
</insert>
<!--添加用户:获取返回主键:方式二-->
<insert id="saveUser2" parameterType="user" >
<!--
selectKey : 适用范围更广,支持所有类型的数据库
order="AFTER" : 设置在sql语句执行前(后),执行此语句
keyColumn="id" : 指定主键对应列名
keyProperty="id":把返回主键的值,封装到实体中的那个属性上
resultType="int":指定主键类型
-->
<selectKey order="AFTER" keyColumn="id" keyProperty="id" resultType="int">
SELECT LAST_INSERT_ID();
</selectKey>
insert into user(username,birthday,sex,address) values(#{username},#{birthday},#{sex},#{address})
</insert>
<!-- 动态sql之if : 多条件查询-->
<select id="findByIdAndUsernameIf" parameterType="user" resultType="com.lagou.domain.User">
select * from user
<!-- test里面写的就是表达式
<where>: 相当于where 1= 1,但是如果没有条件的话,不会拼接上where关键字
-->
<where>
<if test="id != null">
and id = #{id}
</if>
<if test="username !=null">
and username = #{username}
</if>
</where>
</select>
<!--动态sql之set : 动态更新-->
<update id="updateIf" parameterType="user">
update user
<!--<set> : 在更新的时候,会自动添加set关键字,还会去掉最后一个条件的逗号 -->
<set>
<if test="username != null">
username = #{username},
</if>
<if test="birthday != null">
birthday = #{birthday},
</if>
<if test="sex != null">
sex = #{sex},
</if>
<if test="address != null">
address = #{address},
</if>
</set>
where id = #{id}
</update>
<sql id="selectUser">
select * from user
</sql>
<!--动态sql的foreach标签:多值查询:根据多个id值查询用户-->
<select id="findByList" parameterType="list" resultType="user">
<include refid="selectUser"/>
<where>
<!--
collection : 代表要遍历的集合元素,通常写collection或者list
open : 代表语句的开始部分
close : 代表语句的结束部分
item : 代表遍历结合中的每个元素,生成的变量名
separator: 分隔符
-->
<foreach collection="collection" open="id in (" close=")" item="id" separator=",">
#{id}
</foreach>
</where>
</select>
<!--动态sql的foreach标签:多值查询:根据多个id值查询用户-->
<select id="findByArray" parameterType="int" resultType="user">
<include refid="selectUser"/>
<where>
<!--
collection : 代表要遍历的集合元素,通常写collection或者list
open : 代表语句的开始部分
close : 代表语句的结束部分
item : 代表遍历结合中的每个元素,生成的变量名
separator: 分隔符
-->
<foreach collection="array" open="id in (" close=")" item="id" separator=",">
#{id}
</foreach>
</where>
</select>
</mapper>
转载请注明原文地址:https://blackberry.8miu.com/read-1070.html