发表于: 2020-04-24 22:26:48
1 1501
Mybatis
Mybatis将sql和其他java代码分开,只需要将sql写在usermap里
流程:不做session.commit 就会进行回滚
1)配置mybatis-config.xml 全局配置文件 (1.数据源 2.外部的mapper)
2)构建sqlSessionFactory
3)通过sqlSessionFactory构建SqlSession对象
4)通过Sqlsession操作数据库CRUD
5)调用Session.commit提交事务
6)调用Session.close关闭会话
mybatis自动生成UserMapper.xml配置文件:首先build-加generator
参考:https://blog.csdn.net/a656678879/article/details/80469191
修改generator.xml时,project structure-libraries可以找到数据库驱动安装的位置
最终的UserMapper.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可以随便起-->
<!--<mapper namespace="main.java.com.mapper.UserMapper" >-->
<mapper namespace="dao.UserDao" >
<resultMap id="userMap" type="pojo.User" >
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
-->
<id column="id" property="id" jdbcType="BIGINT" />
<result column="name" property="name" jdbcType="VARCHAR" />
<result column="qq" property="qq" jdbcType="VARCHAR" />
<result column="type" property="type" jdbcType="INTEGER" />
<result column="time" property="time" jdbcType="BIGINT" />
<result column="link" property="link" jdbcType="VARCHAR" />
<result column="create_at" property="createAt" jdbcType="BIGINT" />
<result column="create_by" property="createBy" jdbcType="BIGINT" />
<result column="update_at" property="updateAt" jdbcType="BIGINT" />
<result column="update_by" property="updateBy" jdbcType="BIGINT" />
</resultMap>
<sql id="common" >
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
-->
id, name, qq, type, time, link, create_at, create_by, update_at, update_by
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Long" >
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
-->
select
<include refid="common" />
from user
where id = #{id,jdbcType=BIGINT}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Long" >
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
-->
delete from user
where id = #{id,jdbcType=BIGINT}
</delete>
<insert id="addUser" parameterType="pojo.User" useGeneratedKeys="true" keyProperty="id">
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
-->
insert into user (name, qq,
type, time, link, create_at,
create_by, update_at, update_by
)
values ( #{name,jdbcType=VARCHAR}, #{qq,jdbcType=VARCHAR},
#{type,jdbcType=INTEGER}, #{time,jdbcType=BIGINT}, #{link,jdbcType=VARCHAR}, #{createAt,jdbcType=BIGINT},
#{createBy,jdbcType=BIGINT}, #{updateAt,jdbcType=BIGINT}, #{updateBy,jdbcType=BIGINT}
)
</insert>
<insert id="insertSelective" parameterType="pojo.User" >
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
-->
insert into user
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="id != null" >
id,
</if>
<if test="name != null" >
name,
</if>
<if test="qq != null" >
qq,
</if>
<if test="type != null" >
type,
</if>
<if test="time != null" >
time,
</if>
<if test="link != null" >
link,
</if>
<if test="createAt != null" >
create_at,
</if>
<if test="createBy != null" >
create_by,
</if>
<if test="updateAt != null" >
update_at,
</if>
<if test="updateBy != null" >
update_by,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="id != null" >
#{id,jdbcType=BIGINT},
</if>
<if test="name != null" >
#{name,jdbcType=VARCHAR},
</if>
<if test="qq != null" >
#{qq,jdbcType=VARCHAR},
</if>
<if test="type != null" >
#{type,jdbcType=INTEGER},
</if>
<if test="time != null" >
#{time,jdbcType=BIGINT},
</if>
<if test="link != null" >
#{link,jdbcType=VARCHAR},
</if>
<if test="createAt != null" >
#{createAt,jdbcType=BIGINT},
</if>
<if test="createBy != null" >
#{createBy,jdbcType=BIGINT},
</if>
<if test="updateAt != null" >
#{updateAt,jdbcType=BIGINT},
</if>
<if test="updateBy != null" >
#{updateBy,jdbcType=BIGINT},
</if>
</trim>
</insert>
<update id="updateUser" parameterType="pojo.User" >
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
-->
update user
<set >
<if test="name != null" >
name = #{name,jdbcType=VARCHAR},
</if>
<if test="qq != null" >
qq = #{qq,jdbcType=VARCHAR},
</if>
<if test="type != null" >
type = #{type,jdbcType=INTEGER},
</if>
<if test="time != null" >
time = #{time,jdbcType=BIGINT},
</if>
<if test="link != null" >
link = #{link,jdbcType=VARCHAR},
</if>
<if test="createAt != null" >
create_at = #{createAt,jdbcType=BIGINT},
</if>
<if test="createBy != null" >
create_by = #{createBy,jdbcType=BIGINT},
</if>
<if test="updateAt != null" >
update_at = #{updateAt,jdbcType=BIGINT},
</if>
<if test="updateBy != null" >
update_by = #{updateBy,jdbcType=BIGINT},
</if>
</set>
where id = #{id,jdbcType=BIGINT}
</update>
<update id="updateByPrimaryKey" parameterType="pojo.User" >
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
-->
update user
set name = #{name,jdbcType=VARCHAR},
qq = #{qq,jdbcType=VARCHAR},
type = #{type,jdbcType=INTEGER},
time = #{time,jdbcType=BIGINT},
link = #{link,jdbcType=VARCHAR},
create_at = #{createAt,jdbcType=BIGINT},
create_by = #{createBy,jdbcType=BIGINT},
update_at = #{updateAt,jdbcType=BIGINT},
update_by = #{updateBy,jdbcType=BIGINT}
where id = #{id,jdbcType=BIGINT}
</update>
<select id="selectObject" resultMap="pojo.User">
select * from user
where id = {#id}
</select>
<select id="selectList" resultMap="pojo.User">
select <include refid="common"/> from user
</select>
</mapper>
把数据库中的列定义成java代码中的属性,可以进行驼峰映射
<result column="create_at" property="createAt" jdbcType="BIGINT" />
将mybatis-config存为模板文件
下一次日报内容:
sqlSession
UserImpl + UserDao+UserDaoTest
评论