小配置

 <?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. nexus5 root教程

    转载自: http://www.inexus.co/article-1280-1.html http://www.pc6.com/edu/71016.html https://download.cha ...

  2. VirtualBox虚拟机网络设置

    VirtualBox虚拟机网络设置 测试环境:物理机win10企业版本,VirtaulBox版本5.0.14,虚拟机安装Windows XP及linux系统 想实现虚拟机上网的最简单方式,修改虚拟机网 ...

  3. WPF控件---Border应用

    内容模型:Border 只能具有一个子元素.若要显示多个子元素, 需要将一个容器元素放置在父元素Border中. <Grid> <Border BorderBrush="B ...

  4. WPF样式和资源2

    <Window.Resources> <FontFamily x:key="ButtonFontFamily">Time New Roman</Fon ...

  5. ruby.new

    ruby.new 输出:print.puts.p 注释 #say hello =begin this is a long comment =end 变量 local: time or _time in ...

  6. java下文件遍历,与删除

    package cn.stat.p1.file; import java.io.File; public class newfilelist { /** * @param args */ public ...

  7. javascript 操作 css Rule

    //add addCssRule('.bcd',{'color':'red','font-weight':'bold','font-size':'12px'},document.styleSheets ...

  8. jq:get获取json数据并以表格形式生成到页面

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  9. C# 过滤SerialPort端口

    C#中的SerialPort类只能通过SerialPort.GetPortNames()来获取端口名字,而且出来都是COM+数字的字样,如果我要识别COM端口是什么设备的,它就无能为力了,此时我们可以 ...

  10. PHP数组的排序函数

    对保存在数组中的相关数据进行排序是一件非常有意义的事情.在PHP中提供了很多函数可以对数组进行排序,这些函数提供了多种排序的方法.例如,可以通过元素的值或键及自定义排序等. ①简单的数组排序函数简单的 ...