MyBatis初学者配置
小配置
<?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初学者配置的更多相关文章
- Mybatis XML配置
Mybatis常用带有禁用缓存的XML配置 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE ...
- MyBatis Cache配置
@(MyBatis)[Cache] MyBatis Cache配置 MyBatis提供了一级缓存和二级缓存 配置 全局配置 配置 说明 默认值 可选值 cacheEnabled 全局缓存的开关 tru ...
- spring和mybatis整合配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- SSM ( Spring 、 SpringMVC 和 Mybatis )配置详解
使用 SSM ( Spring . SpringMVC 和 Mybatis )已经有三个多月了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,当然肯定有很多可以改进的地方.之前没 ...
- Mybatis中配置Mapper的方法
在这篇文章中我主要想讲一下Mybatis配置文件中mappers元素的配置.关于基础部分的内容可以参考http://haohaoxuexi.iteye.com/blog/1333271. 我们知道在M ...
- MyBatis 实践 -配置
MyBatis 实践 标签: Java与存储 Configuration mybatis-configuration.xml是MyBatis的全局配置文件(文件名任意),其配置内容和顺序如下: pro ...
- SpringMVC+Mybatis+MySQL配置Redis缓存
SpringMVC+Mybatis+MySQL配置Redis缓存 1.准备环境: SpringMVC:spring-framework-4.3.5.RELEASE-dist Mybatis:3.4.2 ...
- spring整合mybatis(hibernate)配置
一.Spring整合配置Mybatis spring整合mybatis可以不需要mybatis-config.xml配置文件,直接通过spring配置文件一步到位.一般需要具备如下几个基本配置. 1. ...
- 笔记:MyBatis XML配置详解
MyBatis 的配置文件包含了影响 MyBatis 行为甚深的设置(settings)和属性(properties)信息.文档的顶层结构如下: configuration 配置 properties ...
随机推荐
- 基于年纪和成本(Age & Cost)的缓存替换(cache replacement)机制
一.客户端的缓存与缓存替换机制 客户端的资源缓存: 在客户端游戏中,通常有大量的资源要处理,这些可能包括贴图.动作.模型.特效等等,这些资源往往存在着磁盘文件->内存(->显存)的数据通路 ...
- 魔方公式xyz
x:(整个魔方以R的方向转动),x':(整个魔方以R'的方向转动) y:(整个魔方以U的方向转动),y':(整个魔方以U'的方向转动) z:(整个魔方以F的方向转动),z':(整个魔方以F'的方向 ...
- POJ2431 Expedition(排序+优先队列)
思路:先把加油站按升序排列. 在经过加油站时.往优先队列里增加B[i].(每经过一个加油站时,预存储一下油量) 当油箱空时:1.假设队列为空(能够理解成预存储的油量),则无法到达下一个加油站,更无法到 ...
- 苹果有益让老iPhone变慢以迫使消费者购买新一代的iPhone?
首先,来一组来自谷歌Trends的图片.(谷歌Trends记录了某段时间内相关关键词搜索的次数.) 假设你做数据,那么你应该会有些感觉. 特别是第一幅图,它规律似乎比第二幅更明显,第二幅图仅仅是一个普 ...
- CentOS yum Fatal Error 处理一例
环境说明 [root@thatsit ~]# cat /etc/redhat-release CentOS Linux release (Core) [root@thatsit ~]# uname - ...
- Set,Map数据结构
/*Set : 多个value的集合, value不重复Map : 多个key-value对的集合, key不重复 1. Set容器 1). Set() 2). Set(array) 3). add( ...
- 附加数据库报错:无法打开物理文件 XXX.mdf",操作系统错误 5:"5(拒绝访问。)"
今天在附加数据库的时候出现如图报错信息: 无法打开物理文件 XXX.mdf",操作系统错误 5:"5(拒绝访问.)"错信息如图:(是不是远程服务器数据库附加出现只读那个情 ...
- a标签的onclick和href事件的区别
在执行顺序上href是低于onclick的,那么这个会造成什么影响呢 <div onclick="a()"> <a href="#" oncl ...
- myEclipse笔记(1):优化配置
一.设置字体 Window->Preferences->General->Appearance->Colors and Fonts 在右侧找到”Aa Test Font”双击或 ...
- 实现html元素跟随touchmove事件的event.touches[0].clientX移动
主要是使用了transform:translateX 实现 <!DOCTYPE html> <html lang="en"> <head> &l ...