关闭

Mybatis 学习笔记

标签: mybatis
71人阅读 评论(0) 收藏 举报
分类:

.初步接触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>

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:501次
    • 积分:81
    • 等级:
    • 排名:千里之外
    • 原创:8篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档