小配置

 <?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="cn.entity.Dept">
<!-- id:唯一标识:通过此id,程序可唯一锁定一条SQL
parameterType:参数类型
resultType:结果类型
-->
<sql id="columns">
deptid,deptname
</sql>
<!-- 查询部分列*号用别名 -->
<select id="selectSql" resultType="cn.entity.Dept">
select <include refid="columns"></include> from Dept
</select> <resultMap id="Dept" type="cn.entity.Dept">
<result property="deptid" column="deptid"></result>
</resultMap>
<!-- 查询所有部门信息 -->
<select id="selectDept" resultMap="Dept">
select * from Dept
</select>
<!-- 带条件查询 -->
<select id="selectDeptById" parameterType="cn.entity.Dept" resultType="cn.entity.Dept">
select * from Dept where deptid=#{deptid}
</select>
<!-- 添加部门信息 -->
<insert id="insertDept" parameterType="cn.entity.Dept">
insert into Dept(deptname) values(#{deptname})
</insert> <!-- 删除部门信息 -->
<delete id="deleteDept" parameterType="cn.entity.Dept">
delete from Dept where deptid=#{deptid}
</delete>
<!-- 修改部门信息 -->
<update id="updateDept" parameterType="cn.entity.Dept">
update Dept set deptname=#{deptname} where deptid=#{deptid}
</update>
<!-- 模糊查询 -->
<select id="likeDept" resultType="cn.entity.Dept" parameterType="cn.entity.Dept">
select * from Dept where deptname like '%${deptname}%'
</select>
<!-- 智能标签where 动态查询 -->
<select id="selectDeptDynamic" parameterType="cn.entity.Dept" resultType="cn.entity.Dept">
select * from Dept
<where>
<if test="deptid!=null">
and deptid=#{deptid}
</if>
<if test="deptname!=null">
and deptname=#{deptname}
</if>
</where>
</select>
<!-- 智能标签set更新 -->
<update id="updatesetDept" parameterType="cn.entity.Dept" >
update Dept
<set>
<if test="deptid!=null">deptid=#{deptid},</if>
<if test="deptname!=null">deptname=#{deptname},</if>
</set>
where deptid=#{deptid}
</update> </mapper>

大配置

 <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<environments default="development">
<environment id="development">
<!-- 事务策略是JDBC -->
<transactionManager type="JDBC" />
<!-- 数据源的方式 -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://120.25.78.77:3306/zc" />
<property name="username" value="zc" />
<property name="password" value="zc" />
</dataSource>
</environment>
</environments>
<!--映射文件:描述某个实体和数据库表的对应关系 -->
<mappers>
<mapper resource="cn/entity/Dept.xml" />
</mappers>
</configuration>

测试类

 package cn.test;

 import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.List; import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test; import cn.entity.Dept; public class MybatisTest { SqlSession session;
@Before
public void initData() throws FileNotFoundException{
//1.1 SqlSessionFactoryBuilder
SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder(); //1.2 SqlSesssionFactory
Reader reader=new FileReader("src/mybatis-config.xml");
SqlSessionFactory factory = builder.build(reader); //1.3 SqlSession
session= factory.openSession();
}
/**
* 查询所有部门所有信息
* @throws IOException
*/
@Test
public void selectDeptTest() throws IOException
{
List<Dept> list = session.selectList("selectDept");
for (Dept dept : list) {
System.out.println(dept.getDeptname());
}
session.close();
}
/**
* 增加
*/
@Test
public void insertDeptTest()
{
Dept dt=new Dept();
dt.setDeptname("大牛部");
int count =session.insert("insertDept",dt);
System.out.println(count);
session.close(); }
/**
* 带条件查询
*/
@Test
public void selectDeptByIdTest()
{
Dept dt= new Dept();
dt.setDeptid(1);
Dept dept=session.selectOne("selectDeptById",dt);
System.out.println(dept.getDeptname());
session.close(); }
/**
* 删除
*/
@Test
public void deleteDeptTest()
{
Dept dt=new Dept();
dt.setDeptid(3);
int count = session.delete("deleteDept",dt);
session.commit();
System.out.println(count);
session.close();
}
/**
* 修改
*/
@Test
public void updateDeptTest()
{
Dept dt=new Dept();
dt.setDeptid(1);
dt.setDeptname("公关部");
int count = session.update("updateDept",dt);
session.commit();
System.out.println(count);
session.close();
}
/**
* 模糊查询
*
*/
@Test
public void likeDeptTest()
{
Dept dt=new Dept();
dt.setDeptname("公");
List<Dept> list = session.selectList("likeDept",dt);
for (Dept dept : list) {
System.out.println(dept.getDeptname());
}
session.close(); }
/**
* 别名查询
*/
@Test
public void selectSqlTest() {
List<Dept> list = session.selectList("selectSql");
for (Dept dept : list) {
System.out.println(dept.getDeptname());
}
session.close(); }
/**
* 智能标签where进行动态查询
*/ @Test
public void dynamicWhereTest() throws Exception{
Dept dept=new Dept();
//代表前台用户同时使用 部门编号 和 部门名称 发起了 检索
dept.setDeptname("公关部");
dept.setDeptid(1); List<Dept> list=session.selectList("selectDeptDynamic",dept);
for (Dept dt : list) {
System.out.println(dt.getDeptname());
}
session.close();
}
/**
* 智能标签set更新
*
*/
@Test
public void updatesetDeptTest()
{
Dept dt=new Dept();
dt.setDeptid(1);
dt.setDeptname("公布部");
int count = session.update("updatesetDept",dt);
session.commit();
System.out.println(count);
session.close();
} }

MyBatis初学者配置的更多相关文章

  1. Mybatis XML配置

    Mybatis常用带有禁用缓存的XML配置 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE ...

  2. MyBatis Cache配置

    @(MyBatis)[Cache] MyBatis Cache配置 MyBatis提供了一级缓存和二级缓存 配置 全局配置 配置 说明 默认值 可选值 cacheEnabled 全局缓存的开关 tru ...

  3. spring和mybatis整合配置

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  4. SSM ( Spring 、 SpringMVC 和 Mybatis )配置详解

    使用 SSM ( Spring . SpringMVC 和 Mybatis )已经有三个多月了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,当然肯定有很多可以改进的地方.之前没 ...

  5. Mybatis中配置Mapper的方法

    在这篇文章中我主要想讲一下Mybatis配置文件中mappers元素的配置.关于基础部分的内容可以参考http://haohaoxuexi.iteye.com/blog/1333271. 我们知道在M ...

  6. MyBatis 实践 -配置

    MyBatis 实践 标签: Java与存储 Configuration mybatis-configuration.xml是MyBatis的全局配置文件(文件名任意),其配置内容和顺序如下: pro ...

  7. SpringMVC+Mybatis+MySQL配置Redis缓存

    SpringMVC+Mybatis+MySQL配置Redis缓存 1.准备环境: SpringMVC:spring-framework-4.3.5.RELEASE-dist Mybatis:3.4.2 ...

  8. spring整合mybatis(hibernate)配置

    一.Spring整合配置Mybatis spring整合mybatis可以不需要mybatis-config.xml配置文件,直接通过spring配置文件一步到位.一般需要具备如下几个基本配置. 1. ...

  9. 笔记:MyBatis XML配置详解

    MyBatis 的配置文件包含了影响 MyBatis 行为甚深的设置(settings)和属性(properties)信息.文档的顶层结构如下: configuration 配置 properties ...

随机推荐

  1. Nginx的10万并发内核参数优化

    关于内核参数的优化: net.ipv4.tcp_max_tw_buckets = 6000timewait的数量,默认是180000.net.ipv4.ip_local_port_range = 10 ...

  2. c语言数组小练习

    //查找数组中最大的值: #include<stdio.h> int main01() { , , , , , , , , , ,,}; ]; int i; ;i < ]);i++) ...

  3. DataGrid( 数据表格) 组件[2]

    本节课重点了解 EasyUI 中 DataGrid(数据表格)组件的使用方法,这个组件依赖于Panel(面板).Resizeable(调整大小).LinkButton(按钮).Pageination( ...

  4. XAML 概述

    我们将向 Windows 运行时应用开发人员介绍 XAML 语言和 XAML 概念,并介绍在使用 XAML 创建 Windows 运行时应用时,在 XAML 中声明对象和设置属性的不同方式. 什么是 ...

  5. linux 线程备忘

    三种线程同步机制 •互斥锁 •信号量 •条件变量   pthread_t thread_id; 主要函数 pthread_create(),pthread_exit(),pthread_join(), ...

  6. FANTASY:In which way do you think the world will end?

    In which way do you think the world will end? The moment you are reading my essay, you are somehow c ...

  7. 3.class文件基本结构

    转 http://blog.csdn.net/luanlouis/article/details/39892027 [last updated: 2014/11/19 09:06] 作为Java程序猿 ...

  8. 使用Jquery解决Asp.Net中下拉列表值改变后访问服务器刷新界面。

    使用DropDownList控件时,改变选项时,获取服务端数据库数据并刷新界面数据. 1. 绑定DropDownList控件SelectedIndexChanged事件. 2. AutoPortBac ...

  9. mysql windows 下导入大文件

    先进入你的mysql bin目录 cd D:/php/mysql/bin 输入命令 mysql -u 用户名 -p 密码 数据库名 < 文件路径                          ...

  10. Python新手学习基础之循环语句——While循环

    while循环 上一节的条件语句实际上只能执行一次,如果要反复的判断执行一些事件要怎么办? 这个时候就需要靠while.for等循环语句了. 我们先来认识下while循环,何为while循环?就是在某 ...