mybatis学习总结(三)——增删查改
映射器是mybatis的基础和核心,下面学习下映射器的使用
映射器的主要元素
- select 查询语句,可以自定义参数和返回结果集
- insert 插入语句,返回一个整数,代表插入的条数
- update 更新语句,返回一个整数,代表更新的条数
- delete 删除语句,返回一个整数,代表删除的条数
- parameterMap 定义参数映射关系
- sql 定义一部分sql,然后在其他地方引用
- resultMap 从数据库结果集中来加载对象
select元素
简单例子
<!-- java接口 public int getCount(String name); --> <select id="getCount" resultType="int" parameterType="String" > select count(*) c from t_user where name like concat ('%',#{name},'%') </select>
查询结果返回resultMap,传递多个参数
<!-- java接口 public List<UserPO> getUserByName(Map<String, String> param); --> <select id="getUserByName" parameterType="java.util.Map" resultMap="userMap"> select * from t_user where username like concat ('%',#{name},'%') </select>
使用JavaBean传参
<!-- java接口 public List<UserPO> getUser(UserPO po); --> <select id="getUser" parameterType="com.zyx.demo.test.po.UserPO" resultMap="userMap"> select * from t_user where username like concat ('%',#{username},'%') </select>
insert元素
简单例子,主键回填
<!-- public int insertUser(UserPO user) --> <insert id="insertUser" useGeneratedKeys="true" keyProperty="id"> insert into t_user (username,password,account) values (#{username},#{password},#{account}) </insert>
update和delete
简单例子
<!-- public int updateUser (UserPO user,int id) --> <update id="updateUser" > update t_user set username=#{username},password=#{password},account=#{account} where id=#{id} </update> <!-- public int deleteUser(int id) --> <delete id="deleteUser" parameterType="int"> delete from t_user where id=#{id} </delete>
resultMap结果映射集
<!-- 自定义返回结果集 --> <resultMap id="userMap" type="com.zyx.demo.test.po.UserPO"> <id property="id" column="id" javaType="java.lang.Integer"></id> <result property="username" column="username" javaType="java.lang.String"></result> <result property="password" column="password" javaType="java.lang.String"></result> <result property="account" column="account" javaType="java.lang.Double"></result> </resultMap>
动态SQL
if元素
<select id="getUserByName" parameterType="java.util.Map" resultMap="userMap"> select * from t_user where true <if test="name != null and name!=''" > AND username like concat ('%',#{name},'%') </if> </select>
choose when otherwise 类似于swich case default
<select id="getUserByName" parameterType="java.util.Map" resultMap="userMap"> select * from t_user where true <choose> <when test="name != null and name!=''">AND username like concat ('%',#{name},'%')</when> <otherwise>and true</otherwise> </choose> </select>
where(sql中不用添加where true)
<select id="getUserByName" parameterType="java.util.Map" resultMap="userMap"> select * from t_user <where> <if test="name != null and name!=''" > AND username like concat ('%',#{name},'%') </if> </where> </select>
set(帮助我们去掉错误数据)
<update id="updateUser" > update t_user <set> <if test="username != null and username!=''">username=#{username}</if> <if test="password != null and password!=''">password=#{password}</if> <if test="account != null and account!=''">account=#{account}</if> </set> where id=#{id} </update>
foreach元素是循环语句,作用是遍历集合
test元素用于条件判断的语句中
bind元素的作用是通过OGNL表达式去自定义一个上下文变量
自动生成mapper
下载jar
<!-- 数据库驱动包 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.6</version> </dependency> <!-- mybatis-generator-core核心jar包,代码生成 --> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.2</version> </dependency>
准备generator.xml
<?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="D:\generator\mysql-connector-java-5.1.6.jar" /> <context id="caigouTables" targetRuntime="MyBatis3"> <commentGenerator> <!-- 是否去除自动生成的注释 true:是 : false:否 --> <property name="suppressAllComments" value="true" /> </commentGenerator> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://10.144.55.11:3306/test" userId="root" password="gome"> </jdbcConnection> <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer true,把JDBC DECIMAL 和 NUMERIC 类型解析为java.math.BigDecimal --> <javaTypeResolver> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- targetProject:生成PO类的位置 --> <javaModelGenerator targetPackage="com.pojo" targetProject="D:\generator\src"> <!-- enableSubPackages:是否让schema作为包的后缀 --> <property name="enableSubPackages" value="false" /> <!-- 从数据库返回的值被清理前后的空格 --> <property name="trimStrings" value="true" /> </javaModelGenerator> <!-- targetPackage:mapper映射文件生成的位置 --> <sqlMapGenerator targetPackage="com.mapper" targetProject="D:\generator\src"> <property name="enableSubPackages" value="false" /> </sqlMapGenerator> <!-- targetPackage:mapper接口的生成位置 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.mapper" targetProject="D:\generator\src"> <property name="enableSubPackages" value="false" /> </javaClientGenerator> <!-- 指定表 t_user这里不指定schema,逆向工程会查询sysuser都有哪些schema,对每个schema生成对象--> <table tableName="t_user" domainObjectName="User"></table> </context> </generatorConfiguration>
执行命令,生成代码
java -jar mybatis-generator-core-1.3.2.jar -configfile mybatis-generator.xml