Mybatis 学习笔记

原创 2016年08月30日 11:05:16

.初步接触Mybatis

1.加入jar

mybatis-3.2.6.jar

mysql-connector-java-5.1.36-bin.jar

 

2.配置mybatis-config.xml

小知识: mybatis-config\mybatis-mapper XML 的自动提示需要下载两个dtd

http://mybatis.org/dtd/mybatis-3-config.dtd

http://mybatis.org/dtd/mybatis-3-mapper.dtd

Window >> Preferences >> XML >> XML Catalog>> Add

Location : 下载的文件

Key: URL : 上面的地址

 

例:

<?xmlversion="1.0" encoding="UTF-8"?>

<!DOCTYPEconfiguration PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""mybatis-3-config.dtd" >

<configuration>

<!-- development:开发模式

 work: 工作模式

 

POOLED:关闭数据链接是进入连接池

Unpooled: 是直接关闭

 -->

<environmentsdefault="development">

<environmentid="development">

<transactionManagertype="JDBC"></transactionManager>

<dataSourcetype="POOLED">

<propertyname="driver" value="com.mysql.jdbc.Driver"/>

<propertyname="url" value="jdbc:mysql:///test"/>

<propertyname="username" value="root"/>

<propertyname="password" value="wxniit"/>

</dataSource>

</environment>

</environments>

</configuration>

 

3.配置实体类的映射xml

例:

<?xmlversion="1.0" encoding="UTF-8"?>

<!DOCTYPEmapper SYSTEM " http://mybatis.org/dtd/mybatis-3-mapper.dtd" >

<mappernamespace="com.chenj.app.mapping.department">

  <select id="getDepartment"parameterType="int"resultType="com.chenj.app.entity.Department">

         select * from departmentwhere id = #{id}

  </select>

</mapper>

 

4.mybatis-config.xml中加载实体类 的映射XML

例:

<configuration>

<environmentsdefault="development">

/..

</environments>

 

<mappers>

<mapperresource="com/chenj/app/mapping/department.xml"/>

</mappers>

 

 

</configuration>

 

5.测试

例:

Stringresource="config.xml";

InputStreamis = test.class.getClassLoader().getResourceAsStream(resource);

SqlSessionFactory sqlSessionFactory= new SqlSessionFactoryBuilder().build(is);

SqlSessionsession = sqlSessionFactory.openSession();

Stringstatement="com.chenj.app.mapping.department.getDepartment";

Department  department=session.selectOne(statement, 1);

System.out.println(department);

.Mybatis CRUD 操作

     1.基于XML

配置SQL

<mappernamespace="com.chenj.app.mapping.department">

  <select id="getDepartment"parameterType="int"resultType="com.chenj.app.entity.Department">

         select * from departmentwhere id = #{id}

  </select>

 

  <insert id="addDepartment"parameterType="com.chenj.app.entity.Department">

         insert  into department(id,name) value(#{id},#{name})

  </insert>

 

  <update id="updateDepartment"parameterType="com.chenj.app.entity.Department">

         update department setname=#{name} where id=#{id}

  </update>

 

  <delete id="delDepartment"parameterType="int">

         delete from departmentwhere id=#{id}

  </delete>

 

  <select id="getAllDepartment"resultType="com.chenj.app.entity.Department">

         select * from department

  </select>

</mapper>

 

 

测试用例

---------------------------------------获取SqlSessionFactory的工具

publicclass MybatisUtils {

 

publicstatic SqlSessionFactory getFactory(){

InputStreamis = test.class.getClassLoader().getResourceAsStream("config.xml");

SqlSessionFactoryfactory = new SqlSessionFactoryBuilder().build(is);

returnfactory;

}

 

}

----------------------------------------测试

 

@Test

publicvoid addDepartment() {

SqlSessionFactoryfactory = MybatisUtils.getFactory();

SqlSessionsession = factory.openSession(true);

Stringstatement = "com.chenj.app.mapping.department.addDepartment";

intinsert=session.insert(statement, new Department(3,"aa"));

System.out.println(insert);

}

 

@Test

publicvoid updateDepartment(){

SqlSessionFactoryfactory = MybatisUtils.getFactory();

SqlSessionsession = factory.openSession(true);

Stringstatement = "com.chenj.app.mapping.department.updateDepartment";

intupdate=session.update(statement, new Department(3,"bb"));

System.out.println(update);

}

 

@Test

publicvoid delDepartment(){

SqlSessionFactoryfactory = MybatisUtils.getFactory();

SqlSessionsession = factory.openSession(true);

Stringstatement = "com.chenj.app.mapping.department.delDepartment";

intdelete=session.delete(statement, 3);

System.out.println(delete);

}

 

@Test

publicvoid findDepartmentByID(){

SqlSessionFactoryfactory = MybatisUtils.getFactory();

SqlSessionsession = factory.openSession(true);

Stringstatement = "com.chenj.app.mapping.department.getDepartment";

Departmentdepartment=session.selectOne(statement, 1);

System.out.println(department);

}

 

@Test

publicvoid findAllDepartment(){

SqlSessionFactoryfactory = MybatisUtils.getFactory();

SqlSessionsession = factory.openSession(true);

Stringstatement = "com.chenj.app.mapping.department.getAllDepartment";

List<Department>result = session.selectList(statement);

System.out.println(result);

}

 

 

  2.基于注解

2.1定义接口,无需实现类

publicinterface Department {

 

@Select(value= { "select * from department where id = #{id}" })

publiccom.chenj.app.entity.Department getDepartment(int id);

 

@Update("updatedepartment set name=#{name} where id=#{id}")

publicint udpateDepartment(com.chenj.app.entity.Department department);

 

@Insert("insert  into department(id,name) value(#{id},#{name})")

publicint addDepartment(com.chenj.app.entity.Department department);

 

@Delete("deletefrom department where id=#{id}")

publicint delDepartment(int id);

 

@Select("select* from department")

publicList<com.chenj.app.entity.Department> getAllDepartment();

 

}

 

2.2主配置中加载这个接口类mapping

<mappers>

<!--<mapperresource="com/chenj/app/mapping/department.xml"/>-->

<mapper class="com.chenj.app.mapper.Department"/>

</mappers>

 

2.3测试

主要是通过 SqlSession.getMapper(接口.class) 获取这个接口

@Test

publicvoid add(){

SqlSessionFactoryfactory = MybatisUtils.getFactory();

SqlSessionsession = factory.openSession(true);

com.chenj.app.mapper.Departmentdepartment = session.getMapper(com.chenj.app.mapper.Department.class);

intadd=department.addDepartment(new Department(5,"aaaa"));

System.out.println(add);

session.close();

}

 

 

 

3.取别名

<typeAliases>

    <packagename="com.chenj.app.entity"/>

<!--<typeAlias type="com.chenj.app.entity.Department"alias="department"/> -->

</typeAliases>

 

当使用package的时候别名为类的简名称

 

  1. 一对一联表

例:

 <select id="getEmployee"parameterType="int" resultMap="getEmployeeMap">

                  select*,d.name dname from employee e left join department d on e.departmentid=d.idwhere e.id = #{id}

   </select>

   <resultMap type="Employee"id="getEmployeeMap">

                  <idproperty="id" column="id"/>

                  <resultproperty="name" column="name"/>

                  <resultproperty="salary" column="salary"/>

                  <associationproperty="department" javaType="Department">

                          <idproperty="id" column="departmentid"/>

                          <resultproperty="name" column="dname"/>

                  </association>

   </resultMap>

 

  1. 一对多

resultMap 里加 collection

例:

      <collectionproperty="list" ofType="Student">

                          <id/>

                          <result/>

                  </collection>

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

mybatis学习笔记

  • 2015年12月09日 17:26
  • 2KB
  • 下载

mybatis 学习笔记

  • 2016年11月16日 00:21
  • 70KB
  • 下载

mybatis学习笔记(12)-多对多查询

本文实现多对多查询,查询用户及用户购买商品信息。示例查询主表是:用户表关联表:由于用户和商品没有直接关联,通过订单和订单明细进行关联,所以关联表:orders、orderdetail、items...

MyBatis学习笔记

  • 2017年03月12日 16:45
  • 2.44MB
  • 下载

MyBatis学习笔记

  • 2016年02月21日 23:34
  • 3KB
  • 下载

J2EE系列之MyBatis学习笔记(十一)-- 使用注解配置sql映射器

这里讲一下使用注解方式实现mybatis的关系映射(一对一映射和一对多映射) 一、一对一映射 1.新建类Address(直接从前面讲使用xml配置方式实现关系映射的工程中复制即可): packa...

SpringMVC-MyBatis学习笔记

  • 2017年04月27日 09:51
  • 566KB
  • 下载

mybatis完整学习笔记

  • 2017年08月08日 10:14
  • 4.06MB
  • 下载

springmvc学习笔记(21)-springmvc整合mybatis遇到的问题及解决小结

本文主要记录springmvc整合mybatis整合过程中遇到的各种问题和解决方法 遇到的问题 在web.xml中标签报红 BeanCreationException mybatis绑定错误 请求参...

mybatis学习笔记

  • 2012年12月23日 21:04
  • 1.68MB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Mybatis 学习笔记
举报原因:
原因补充:

(最多只允许输入30个字)