mybatis配置
一、创建Maven工程并编辑pom.xml
pom.xml
<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.0http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>mabatis_v1.0</groupId>
<artifactId>mabatis_v1.0</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
<!-- 依赖关系 -->
<dependencies>
<!-- ibatis.jar 从网络获取,则不需要手动导入ibatis的jar包!-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.8</version>
</dependency>
<!-- junit.jar -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
</dependency>
</dependencies>
</project>
二、创建java类

三、创建java对象与数据库之间的xml文件
NewsMapper.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.hedong.dao.NewsMapper" >
<resultMap id="BaseResultMap" type="com.hedong.pojo.News" >
<id column="newsID" property="newsid" jdbcType="INTEGER" />
<result column="newsType" property="newstype" jdbcType="VARCHAR" />
<result column="newsTitle" property="newstitle" jdbcType="VARCHAR" />
<result column="newsWriter" property="newswriter" jdbcType="VARCHAR" />
<result column="newsTime" property="newstime" jdbcType="TIMESTAMP" />
</resultMap>
<resultMap id="ResultMapWithBLOBs" type="com.hedong.pojo.News" extends="BaseResultMap" >
<result column="newsContent" property="newscontent" jdbcType="LONGVARCHAR" />
</resultMap>
<sql id="Example_Where_Clause" >
<where >
<foreach collection="oredCriteria" item="criteria" separator="or" >
<if test="criteria.valid" >
<trim prefix="(" suffix=")" prefixOverrides="and" >
<foreach collection="criteria.criteria" item="criterion" >
<choose >
<when test="criterion.noValue" >
and ${criterion.condition}
</when>
<when test="criterion.singleValue" >
and ${criterion.condition} #{criterion.value}
</when>
<when test="criterion.betweenValue" >
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
</when>
<when test="criterion.listValue" >
and ${criterion.condition}
<foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >
#{listItem}
</foreach>
</when>
</choose>
</foreach>
</trim>
</if>
</foreach>
</where>
</sql>
<sql id="Update_By_Example_Where_Clause" >
<where >
<foreach collection="example.oredCriteria" item="criteria" separator="or" >
<if test="criteria.valid" >
<trim prefix="(" suffix=")" prefixOverrides="and" >
<foreach collection="criteria.criteria" item="criterion" >
<choose >
<when test="criterion.noValue" >
and ${criterion.condition}
</when>
<when test="criterion.singleValue" >
and ${criterion.condition} #{criterion.value}
</when>
<when test="criterion.betweenValue" >
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
</when>
<when test="criterion.listValue" >
and ${criterion.condition}
<foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >
#{listItem}
</foreach>
</when>
</choose>
</foreach>
</trim>
</if>
</foreach>
</where>
</sql>
<sql id="Base_Column_List" >
newsID, newsType, newsTitle, newsWriter, newsTime
</sql>
<sql id="Blob_Column_List" >
newsContent
</sql>
<select id="selectByExampleWithBLOBs" resultMap="ResultMapWithBLOBs" parameterType="com.hedong.pojo.NewsExample" >
select
<if test="distinct" >
distinct
</if>
<include refid="Base_Column_List" />
,
<include refid="Blob_Column_List" />
from news_tb
<if test="_parameter != null" >
<include refid="Example_Where_Clause" />
</if>
<if test="orderByClause != null" >
order by ${orderByClause}
</if>
</select>
<select id="selectByExample" resultMap="BaseResultMap" parameterType="com.hedong.pojo.NewsExample" >
select
<if test="distinct" >
distinct
</if>
<include refid="Base_Column_List" />
from news_tb
<if test="_parameter != null" >
<include refid="Example_Where_Clause" />
</if>
<if test="orderByClause != null" >
order by ${orderByClause}
</if>
</select>
<select id="selectByPrimaryKey" resultMap="ResultMapWithBLOBs" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
,
<include refid="Blob_Column_List" />
from news_tb
where newsID = #{newsid,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
delete from news_tb
where newsID = #{newsid,jdbcType=INTEGER}
</delete>
<delete id="deleteByExample" parameterType="com.hedong.pojo.NewsExample" >
delete from news_tb
<if test="_parameter != null" >
<include refid="Example_Where_Clause" />
</if>
</delete>
<insert id="insert" parameterType="com.hedong.pojo.News" >
insert into news_tb (newsID, newsType, newsTitle,
newsWriter, newsTime, newsContent
)
values (#{newsid,jdbcType=INTEGER}, #{newstype,jdbcType=VARCHAR}, #{newstitle,jdbcType=VARCHAR},
#{newswriter,jdbcType=VARCHAR}, #{newstime,jdbcType=TIMESTAMP}, #{newscontent,jdbcType=LONGVARCHAR}
)
</insert>
<insert id="insertSelective" parameterType="com.hedong.pojo.News" >
insert into news_tb
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="newsid != null" >
newsID,
</if>
<if test="newstype != null" >
newsType,
</if>
<if test="newstitle != null" >
newsTitle,
</if>
<if test="newswriter != null" >
newsWriter,
</if>
<if test="newstime != null" >
newsTime,
</if>
<if test="newscontent != null" >
newsContent,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="newsid != null" >
#{newsid,jdbcType=INTEGER},
</if>
<if test="newstype != null" >
#{newstype,jdbcType=VARCHAR},
</if>
<if test="newstitle != null" >
#{newstitle,jdbcType=VARCHAR},
</if>
<if test="newswriter != null" >
#{newswriter,jdbcType=VARCHAR},
</if>
<if test="newstime != null" >
#{newstime,jdbcType=TIMESTAMP},
</if>
<if test="newscontent != null" >
#{newscontent,jdbcType=LONGVARCHAR},
</if>
</trim>
</insert>
<select id="countByExample" parameterType="com.hedong.pojo.NewsExample" resultType="java.lang.Integer" >
select count(*) from news_tb
<if test="_parameter != null" >
<include refid="Example_Where_Clause" />
</if>
</select>
<update id="updateByExampleSelective" parameterType="map" >
update news_tb
<set >
<if test="record.newsid != null" >
newsID = #{record.newsid,jdbcType=INTEGER},
</if>
<if test="record.newstype != null" >
newsType = #{record.newstype,jdbcType=VARCHAR},
</if>
<if test="record.newstitle != null" >
newsTitle = #{record.newstitle,jdbcType=VARCHAR},
</if>
<if test="record.newswriter != null" >
newsWriter = #{record.newswriter,jdbcType=VARCHAR},
</if>
<if test="record.newstime != null" >
newsTime = #{record.newstime,jdbcType=TIMESTAMP},
</if>
<if test="record.newscontent != null" >
newsContent = #{record.newscontent,jdbcType=LONGVARCHAR},
</if>
</set>
<if test="_parameter != null" >
<include refid="Update_By_Example_Where_Clause" />
</if>
</update>
<update id="updateByExampleWithBLOBs" parameterType="map" >
update news_tb
set newsID = #{record.newsid,jdbcType=INTEGER},
newsType = #{record.newstype,jdbcType=VARCHAR},
newsTitle = #{record.newstitle,jdbcType=VARCHAR},
newsWriter = #{record.newswriter,jdbcType=VARCHAR},
newsTime = #{record.newstime,jdbcType=TIMESTAMP},
newsContent = #{record.newscontent,jdbcType=LONGVARCHAR}
<if test="_parameter != null" >
<include refid="Update_By_Example_Where_Clause" />
</if>
</update>
<update id="updateByExample" parameterType="map" >
update news_tb
set newsID = #{record.newsid,jdbcType=INTEGER},
newsType = #{record.newstype,jdbcType=VARCHAR},
newsTitle = #{record.newstitle,jdbcType=VARCHAR},
newsWriter = #{record.newswriter,jdbcType=VARCHAR},
newsTime = #{record.newstime,jdbcType=TIMESTAMP}
<if test="_parameter != null" >
<include refid="Update_By_Example_Where_Clause" />
</if>
</update>
<update id="updateByPrimaryKeySelective" parameterType="com.hedong.pojo.News" >
update news_tb
<set >
<if test="newstype != null" >
newsType = #{newstype,jdbcType=VARCHAR},
</if>
<if test="newstitle != null" >
newsTitle = #{newstitle,jdbcType=VARCHAR},
</if>
<if test="newswriter != null" >
newsWriter = #{newswriter,jdbcType=VARCHAR},
</if>
<if test="newstime != null" >
newsTime = #{newstime,jdbcType=TIMESTAMP},
</if>
<if test="newscontent != null" >
newsContent = #{newscontent,jdbcType=LONGVARCHAR},
</if>
</set>
where newsID = #{newsid,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKeyWithBLOBs" parameterType="com.hedong.pojo.News" >
update news_tb
set newsType = #{newstype,jdbcType=VARCHAR},
newsTitle = #{newstitle,jdbcType=VARCHAR},
newsWriter = #{newswriter,jdbcType=VARCHAR},
newsTime = #{newstime,jdbcType=TIMESTAMP},
newsContent = #{newscontent,jdbcType=LONGVARCHAR}
where newsID = #{newsid,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.hedong.pojo.News" >
update news_tb
set newsType = #{newstype,jdbcType=VARCHAR},
newsTitle = #{newstitle,jdbcType=VARCHAR},
newsWriter = #{newswriter,jdbcType=VARCHAR},
newsTime = #{newstime,jdbcType=TIMESTAMP}
where newsID = #{newsid,jdbcType=INTEGER}
</update>
</mapper>
四、创建confg.xml文件

五、创建SqlSessionFactory,获取session


浙公网安备 33010602011771号