Mybatis Notes

Mybatis First

创建Maven项目

配置依赖

<dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>RELEASE</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>RELEASE</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>RELEASE</version></dependency><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>RELEASE</version></dependency>
</dependencies>

log4j配置

src\main\resources\log4j.properties

log4j.rootLogger=DEBUG
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.Target=System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = [%-5p] %m%n
log4j.logger.test=debug,console

jdbc配置文件

src\main\resources\jdbc.properties

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2B8
jdbc.name=root
jdbc.password=123456

主配置文件

src\main\resources\mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration><!--导入jdbc配置文件--><properties resource="jdbc.properties" /><!--别名--><typeAliases><!--两种方式--><!--<typeAlias type="com.hex.model.Student" alias="xxx" />--><package name="com.hex.model" /></typeAliases><!--配置数据库环境--><environments default="mysql_env"><environment id="mysql_env"><transactionManager type="JDBC"/><dataSource type="POOLED"><property name="driver" value="${jdbc.driver}"/><property name="url" value="${jdbc.url}"/><property name="username" value="${jdbc.name}"/><property name="password" value="${jdbc.password}"/></dataSource></environment></environments><!--导入sql映射文件--><mappers><mapper resource="mapper.xml"/></mappers>
</configuration>

mapper映射文件

src\main\resources\mapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hex.model.Student"><!--values里面填的是属性(setget方法名去掉setget首字母小写)--><insert id="insertStudent" parameterType="Student">insert into student(name,age,score) values (#{name},#{age},#{score})</insert><delete id="deleteStudentById" parameterType="int">DELETE FROM student WHERE id=#{id}</delete><delete id="deleteStudentByName" parameterType="string">DELETE FROM student WHERE NAME=#{name}</delete><select id="searchStudentById" parameterType="int" resultType="Student">SELECT * FROM student WHERE id=#{id}</select><select id="searchStudentByName" parameterType="string" resultType="Student">SELECT * FROM student WHERE NAME=#{name}</select><select id="modifyStudent" parameterType="Student">UPDATE student SET NAME=#{name},age=#{age},score=#{score} where id=#{id}</select><select id="fuzzyQueryByName" parameterType="string" resultType="Student">SELECT * FROM student WHERE NAME LIKE '%${value}%'</select>
</mapper>

实体类

com.hex.model.Student

public class Student {private Integer id;private String name;private int age;private double score;public Student() {}public Student(String name, int age, double score) {this.name = name;this.age = age;this.score = score;}/* 省略set、get方法 */
}

dao层

com.hex.dao.StudentDao

package com.hex.dao;
import com.hex.model.Student;
import com.hex.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import javax.sound.midi.SoundbankResource;
import java.util.ArrayList;
import java.util.List;
public class StudentDao {private SqlSession sqlSession;public void insertStudent(Student student){try{sqlSession = MybatisUtils.sqlSessionFactory();sqlSession.insert("insertStudent",student);sqlSession.commit();}finally {sqlSession.close();}}public void deleteStudentById(int id){try{sqlSession = MybatisUtils.sqlSessionFactory();sqlSession.delete("deleteStudentById",id);sqlSession.commit();}finally {sqlSession.close();}}public void deleteStudentByName(String name){try{sqlSession = MybatisUtils.sqlSessionFactory();sqlSession.delete("deleteStudentByName",name);sqlSession.commit();}finally {sqlSession.close();}}public List<Student> searchStudentById(int id){List<Student> studentList  = new ArrayList<Student>();try{sqlSession = MybatisUtils.sqlSessionFactory();studentList = sqlSession.selectList("searchStudentById",id);}finally {sqlSession.close();return studentList;}}public List<Student> searchStudentByName(String name){List<Student> studentList  = new ArrayList<Student>();try{sqlSession = MybatisUtils.sqlSessionFactory();studentList = sqlSession.selectList("searchStudentByName",name);}finally {sqlSession.close();return studentList;}}public void modifyStudent(Student student){try{sqlSession = MybatisUtils.sqlSessionFactory();sqlSession.update("modifyStudent",student);sqlSession.commit();}finally {sqlSession.close();}}public List<Student> fuzzyQueryByName(String word){List<Student> studentList  = new ArrayList<Student>();try{sqlSession = MybatisUtils.sqlSessionFactory();studentList = sqlSession.selectList("fuzzyQueryByName",word);}finally {sqlSession.close();return studentList;}}
}

工具类

com.hex.utils.MybatisUtils

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.InputStream;
public class MybatisUtils {private static SqlSessionFactory sqlSessionFactory;/*** 读取主配置文件,创建SqlSession,如果SqlSession已经存在则直接返回* @return*/public static SqlSession sqlSessionFactory() {try {InputStream is = Resources.getResourceAsStream("mybatis-config.xml");if (sqlSessionFactory == null) {sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);}return sqlSessionFactory.openSession();} catch (IOException e) {e.printStackTrace();}return null;}
}

测试类

com.hex.test.MyTest

import com.hex.dao.StudentDao;
import com.hex.model.Student;
import org.junit.Test;
import java.io.IOException;
public class MyTest {private StudentDao dao = new StudentDao();@Testpublic void test() throws IOException {Student student = new Student("hex",23,98.5);dao.insertStudent(student);}/*省略其他测试方法*/
}

单表的CURD操作

sql语句拼接

<select id="fuzzyQueryByName" parameterType="string" resultType="Student">SELECT * FROM student WHERE NAME like '%' #{word} '%'
</select>
<select id="fuzzyQueryByName" parameterType="string" resultType="Student">SELECT * FROM student WHERE NAME like concat('%',#{word},'%')
</select>
<!--第3中方法难以避免sql注入问题-->
<select id="fuzzyQueryByName" parameterType="string" resultType="Student">SELECT * FROM student WHERE NAME LIKE '%${value}%'
</select>

resultMap

<resultMap id="studentMapper" type="Student"><id column="tid" property="id" /><result column="tname" property="name" />
</resultMap>
<select id="searchStudentById" resultMap="studentMapper">SELECT tid,tname,tage,score FROM student WHERE id=#{id}
</select>

数据库字段名和实体类属性名不一致:将数据库的tid、tname、tage、score封装到Student(id,name,age,score)对象中

mapper动态代理

一般流程
dao对象调用dao层接口的实现类 -> sqlSession.insert(映射器id,映射器需要的参数) -> mapper中对应id的sql语句
mapper的动态代理
new sqlSession -> dao = sqlSession.getMapper(dao层接口.class) -> dao调用接口

通常dao层接口的实现类里面定义了sqlSession去指定mapper.xml映射文件中的哪一个映射方法。然后mapper的动态代理要设置mapper的命名空间,sqlSession.getMapper(dao层接口.class)就相当于指定要找该指定命名空间的映射器。可是映射文件里面定义了那么多select、insert等查询语句,怎么调用我要的呢?这个时候就可以直接用dao对象调用接口,因为接口名和sql语句名是相同的,就可以定位到相应的sql语句。看上去好像是dao直接执行sql语句一样。

两个关键:mapper的命名空间是接口类的全限定类名。接口的方法名和mapper中定义的查询语句的id要保持一致。

@Test
public void mapperDynamicProxy(){SqlSession sqlSession = MybatisUtils.sqlSessionFactory();StudentDao dao = sqlSession.getMapper(StudentDao.class);Student student = dao.selectStudentById(1);System.out.println(student.getName());
}

多条件查询

  • 把查询条件封装成实体类的对象
  • 把查询条件封装成Map
  • 索引
#{}中可以放什么内容:
1.参数对象的属性
2.随意内容,此时的#{}是个占位符
3.参数为Map时的key
4.参数为Map时key所对应的value为对象,则可将该对象的属性放入
5.参数的索引号

动态SQL

原符号 < <= > >= & ' "
替换符号 &lt; &lt;= &gt; &gt;= &amp; &apos; &quot;
  • if

    <select id="selectStudentByCondition" resultType="Student">select * form student<where><if test="name != null and name != ''">and name like '%' #{name} '%'</if><if test="age &gt; 0">and age > #{age}</if></where>
    </select>
  • choose, when, otherwise

    <select id="selectStudentByCondition" resultType="Student">select * form student<where><choose><when test="name != null and name != ''">and name like '%' #{name} '%'</when><when test="age &gt; 0">and age > #{age}</when><otherwise>1 = 2</otherwise></choose></where>
    </select>
  • trim, where, set

  • foreach

    <!--遍历数组-->
    <select id="selectStudentByCondition" resultType="Student">select * form student<if test="array.length > 0">where id in<foreach collection="array" item="myid" open="(" close=")" separator=",">#{myid}</foreach></if>
    </select>
    <!--遍历list-->
    <select id="selectStudentByCondition" resultType="Student">select * form student<if test="list.size > 0">where id in<foreach collection="list" item="myid" open="(" close=")" separator=",">#{myid}</foreach></if>
    </select>
  • bind

sql片段

<sql id="selectColumns">id,name,age,score</sql>
<select id="selectStudentByCondition" resultType="Student">select <include refid="selectColumns" /> form student
</select>

关联关系查询

一对多

创建数据表和实体类

country minister
结构 cid、cname mid、mname、countryid
关系

mapper映射文件

<mapper namespace="com.one2many.dao.CountryDao"><resultMap id="countryMapper" type="Country"><id column="cid" property="cid" /><result column="cname" property="cname"/><collection property="ministers" ofType="Minister"><id column="mid" property="mid"/><result column="mname" property="mname" /></collection></resultMap><select id="selectCountryById" resultMap="countryMapper" parameterType="int">SELECT cid,cname,mid,mname FROM country,minister WHERE cid = countryid and cid = #{id}</select>
</mapper>

测试类

@Test
public void test01(){SqlSession sqlSession = MybatisUtils.sqlSessionFactory();CountryDao dao = sqlSession.getMapper(CountryDao.class);Country country = dao.selectCountryById(1);System.out.println(country.toString());
}

mapper还可以这样写,一个集合的值是从另外一个select语句的结果得到的

<mapper namespace="com.one2many.dao.CountryDao"><select id="selectMinisterByCountry" resultType="Minister">select mid,mname from minister where countryid = #{xxx}</select><resultMap id="countryMapper" type="Country"><id column="cid" property="cid" /><result column="cname" property="cname"/><collection property="ministers"ofType="Minister"select="selectMinisterByCountry"column="cid"/></resultMap><select id="selectCountryById" resultMap="countryMapper" parameterType="int">SELECT cid,cname,mid,mname FROM country,minister WHERE cid = countryid and cid = #{id}</select>
</mapper>

多对一

创建数据表和实体类

country minister
结构 cid、cname mid、mname、country
关系

mapper映射文件

<mapper namespace="com.many2one.dao.MinisterDao"><resultMap id="ministerMapper" type="Minister"><id column="mid" property="mid" /><result column="mname" property="mname" /><association property="country" javaType="Country"><!--property映射成javaType--><id column="cid" property="cid" /><result column="cname" property="cname" /></association></resultMap><select id="selectMinisterById" resultMap="ministerMapper">SELECT MID,mname,cid,cname FROM minister,countryWHERE countryid = cid AND MID = #{xxx}</select>
</mapper>

测试类

@Test
public void test01(){SqlSession sqlSession = MybatisUtils.sqlSessionFactory();MinisterDao dao = sqlSession.getMapper(MinisterDao.class);Minister minister = dao.selectMinisterById(2);System.out.println(minister.toString());
}

mapper还可以这样写,一个关联的值是从另外一个select语句的结果得到的

<mapper namespace="com.many2one.dao.MinisterDao"><select id="selectCountryById" resultType="Country">select cid,cname FROM country where cid=#{xxx}</select><resultMap id="ministerMapper" type="Minister"><id column="mid" property="mid" /><result column="mname" property="mname" /><association property="country"javaType="Country"select="selectCountryById" column="countryid"></association></resultMap><select id="selectMinisterById" resultMap="ministerMapper">SELECT MID,mname,countryid FROM minister WHERE MID = #{xxx}</select>
</mapper>

自关联

NewsLabel模型

public class NewsLabel {private int id;private String name;private Set<NewsLabel> children;/*省略set、get、toString方法*/
}
<mapper namespace="com.hex.dao.NewsLabelDao"><resultMap id="newslabelMapper" type="NewsLabel"><id column="id" property="id" /><result column="name" property="name"/><collection property="children"ofType="NewsLabel"select="selectChildrenByParent"column="id" /></resultMap><select id="selectChildrenByParent" resultMap="newslabelMapper">select id,name FROM newslabel where pid=#{xxx}</select>
</mapper>
@Test
public void test01(){SqlSession sqlSession = MybatisUtils.sqlSessionFactory();NewsLabelDao dao = sqlSession.getMapper(NewsLabelDao.class);List<NewsLabel> list = dao.selectChildrenByParent(2);for (NewsLabel li:list) {System.out.println(li.toString());}
}

多对多

延迟加载

<settings><setting name="lazyLoadingEnable" value="false" /><setting name="aggressiveLazyLoading" value="false" />
</settings>

查询缓存

一级缓存、二级缓存

ehcache

  • 导包

    <dependency><groupId>net.sf.ehcache</groupId><artifactId>ehcache</artifactId><version>2.8.3</version>
    </dependency>
    <dependency><groupId>org.mybatis</groupId><artifactId>mybatis-ehcache</artifactId><version>1.0.0</version>
    </dependency>
  • 引入ehcache类
    xml <mapper> <cache type="org.mybatis.caches.ehcache.EhcacheCache" /> </mapper>

  • 导入配置文件

    把ehcache-core里面的配置文件导入项目

Mybatis注解式开发(dao层接口)

  • @Insert(value="")
  • @Delete(value="")
  • @Update(value="")
  • @Select(value="")
<mapper><!--映射文件所在的包--><package name="com.hex.dao" />
</mapper>

转载于:https://www.cnblogs.com/hextech/p/10657533.html

SSM学习(一)Mybatis相关推荐

  1. JavaWeb学习之路——SSM框架之Mybatis(三)

    数据库配置和相关类创建看上篇:JavaWeb学习之路--SSM框架之Mybatis(二) https://blog.csdn.net/kuishao1314aa/article/details/832 ...

  2. SSM框架的搭建学习(1)---MyBatis的环境搭建

    SSM(Spring+SpringMVC+MyBatis)框架为当今最为流行的WEB开发框架之一,基本上涉及数据库的一些增删改查操作都可以借用此框架,本尊此前接的一个小公司关于楼宇空调监控指标的项目就 ...

  3. SSM(Spring+SpringMVC+Mybatis)框架环境搭建(整合步骤)(一)

    SSM(Spring+SpringMVC+Mybatis)框架环境搭建(整合步骤)(一) 1. 前言 最近在写毕设过程中,重新梳理了一遍SSM框架,特此记录一下. 附上源码:https://gitee ...

  4. Java语言开发在线购物推荐网 购物商城推荐系统 基于用户、物品的协同过滤推荐算法 SSM(Spring+SpringMVC+Mybatis)开发框架 大数据、人工智能、机器学习项目开发

    Java语言开发在线购物推荐网 购物商城推荐系统 基于用户.物品的协同过滤推荐算法 SSM(Spring+SpringMVC+Mybatis)开发框架 大数据.人工智能.机器学习项目开发ShopRec ...

  5. Java+SSM(Spring+SpringMVC+Mybatis)个性化购物商城推荐系统 电子商务推荐系统 基于用户、项目、聚类、混合的协同过滤推荐算法WebShopRSMEx 源代码下载

    Java+SSM(Spring+SpringMVC+Mybatis)个性化购物商城推荐系统 电子商务推荐系统 基于用户.项目.聚类.混合的协同过滤推荐算法WebShopRSMEx 源代码下载 一.项目 ...

  6. Java语言开发在线音乐推荐网 音乐推荐系统 网易云音乐爬虫 基于用户、物品的协同过滤推荐算法 SSM(Spring+SpringMVC+Mybatis)框架 大数据、人工智能、机器学习项目开发

    Java语言开发在线音乐推荐网 音乐推荐系统 网易云音乐爬虫 基于用户.物品的协同过滤推荐算法 SSM(Spring+SpringMVC+Mybatis)框架 大数据.人工智能.机器学习项目开发Mus ...

  7. java毕业设计高校学习社区mybatis+源码+调试部署+系统+数据库+lw

    java毕业设计高校学习社区mybatis+源码+调试部署+系统+数据库+lw java毕业设计高校学习社区mybatis+源码+调试部署+系统+数据库+lw 本源码技术栈: 项目架构:B/S架构 开 ...

  8. java计算机毕业设计计算机课程在线培训学习管理系统MyBatis+系统+LW文档+源码+调试部署

    java计算机毕业设计计算机课程在线培训学习管理系统MyBatis+系统+LW文档+源码+调试部署 java计算机毕业设计计算机课程在线培训学习管理系统MyBatis+系统+LW文档+源码+调试部署 ...

  9. 使用Java+SSM(Spring+SpringMVC+Mybatis)如何开发个性化音乐推荐系统 在线音乐推荐网站 基于用户、项目的协同过滤推荐算法实现MusicRecommendSystemWeb

    使用Java+SSM(Spring+SpringMVC+Mybatis)如何开发个性化音乐推荐系统 在线音乐推荐网站 基于用户.项目的协同过滤推荐算法实现MusicRecommendSystemWeb ...

  10. 如何使用Java+SSM(Spring+SpringMVC+Mybatis)开发个性化新闻推荐系统 在线新闻推荐系统 基于用户项目协同过滤、内容、聚类、关联规则推荐算法实现WebNewsRSMEx

    如何使用Java+SSM(Spring+SpringMVC+Mybatis)开发个性化新闻推荐系统 在线新闻推荐系统 基于用户项目协同过滤.内容.聚类.关联规则推荐算法实现WebNewsRSMEx 一 ...

最新文章

  1. 限界分支法(实际上没有剪枝,介绍的是广度优先搜索):01背包问题,队列实现方式(FIFO)
  2. 那么您想做微服务吗? 请观看微服务以防万一
  3. linux备份用户权限
  4. Java 开发环境配置jdk安装教程
  5. oracle日期函数有效,oracle日期处理函数整理
  6. 电费管理系统php,25175水电费管理系统
  7. 股市里的定律-福克兰定律
  8. Python实现最速下降法(The steepest descent method)详细案例
  9. DEV SIT UAT PET SIM PRD PROD常见环境英文缩写含义
  10. 零基础要怎么样学习嵌入式Linux
  11. PHP开发实战权威指南-读书总结
  12. java万年历设计报告_JAVA《万年历系统》课程设计报告附源码.doc
  13. Uibot(来也RPA)电脑中安装了Office和WPS,但Creator只能调起WPS
  14. android之爬取正方教务管理系统获取信息
  15. aviator表达式教程
  16. 视美泰IOT-3288A安卓主板刷机方法
  17. 性能测试如何创造业务价值
  18. win7 “无法定位程序输入点ucrtbase.terminate于动态链接库api-ms-win-crt-runtime-|1-1-0.dll”的解决办法
  19. 远程桌面连接出现由于网络错误,连接被中断,请重新连接到远程计算机错误的解决方法
  20. MarkDown字体颜色设置

热门文章

  1. 注意!!Java-File类
  2. Android性能优化典范
  3. jboss启动之奇葩问题
  4. cocos2d-html5 onEnter init ctor构造函数 ----js特有特性(和c++有一点不一样)
  5. 针对应用开发者的几点建议
  6. PC与手机的IPCP协商
  7. ubuntu9.04换源
  8. InstallShield与Visual Studio
  9. 一切苦恼的都是本质上由于能力不够
  10. 如何在mac上面看充电器的瓦数!