简介
MyBatis 是一款优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。MyBatis 可以使用简单的 XML 或注解来配置和映射原生类型、接口和 Java 的 POJO(Plain Old Java Objects,普通老式 Java 对象)为数据库中的记录。
一、创建Maven工程并编辑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.0modelVersion> <groupId>mabatis_v1.0groupId> <artifactId>mabatis_v1.0artifactId> <version>0.0.1-SNAPSHOTversion> <packaging>warpackaging> <build> <sourceDirectory>srcsourceDirectory> <plugins> <plugin> <artifactId>maven-compiler-pluginartifactId> <version>3.3version> <configuration> <source>1.8source> <target>1.8target> configuration> plugin> <plugin> <artifactId>maven-war-pluginartifactId> <version>2.6version> <configuration> <warSourceDirectory>WebContentwarSourceDirectory> <failOnMissingWebXml>falsefailOnMissingWebXml> configuration> plugin> plugins> build> <dependencies> <dependency> <groupId>org.mybatisgroupId> <artifactId>mybatisartifactId> <version>3.2.8version> dependency> <dependency> <groupId>junitgroupId> <artifactId>junitartifactId> <version>4.10version> 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