EmployeeMapper.xml
<?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.bdqn.dao.EmployeeMapper"> <select id="getEmployeeList" resultType="Employee">
select * from Employee
</select>
<insert id="addEmployee" parameterType="Employee">
insert into employee (sn,name,gender) values (#{sn},#{name},#{gender})
</insert>
<update id="modifyEmployee" parameterType="Employee">
update employee
<set>
<if test="sn != null"> sn = #{sn},</if>
<if test="name != null"> name = #{name},</if>
<if test="gender != null"> gender = #{gender}</if>
</set>
where id = #{id}
</update>
<delete id="deleteEmployee" parameterType="Employee">
delete from employee where id = #{id}
</delete>
</mapper>

Employee.java

public class Employee {
private Integer id;
private String sn;
private String name;
private String gender;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getSn() {
return sn;
}
public void setSn(String sn) {
this.sn = sn;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
} }

测试类

public class EmployeeDaoTest {

    private SqlSessionTemplate sqlSessionTemplate;

    protected ApplicationContext ctx = null;
private EmployeeDaoTest test; private Logger logger = Logger.getLogger(EmployeeDaoTest.class); @Before
public void setUp() throws Exception{
ctx = new ClassPathXmlApplicationContext("applicationContext-mybatis.xml");
test = (EmployeeDaoTest)ctx.getBean("employeeDaoTest");
} @Test
public void getEmployeeListTest() {
List<Employee> list = test.sqlSessionTemplate.selectList("cn.bdqn.dao.EmployeeMapper.getEmployeeList");
logger.debug("testGetEmployeeList---> " + list.size());
} @Test
public void addEmployeeTest(){
Employee employee = new Employee();
employee.setSn("CESHI");
employee.setName("测试");
employee.setGender("男");
int flag = test.sqlSessionTemplate.insert("cn.bdqn.dao.EmployeeMapper.addEmployee", employee);
Assert.assertEquals(1, flag);
} @Test
public void modifyEmployeeTest(){
Employee employee = new Employee();
employee.setId(8);
employee.setSn("CESHI888");
employee.setName("测试888");
employee.setGender("男");
int flag = test.sqlSessionTemplate.update("cn.bdqn.dao.EmployeeMapper.modifyEmployee", employee);
Assert.assertEquals(1, flag);
} @Test
public void deleteEmployeeTest(){
Employee employee = new Employee();
employee.setId(8);
int flag = test.sqlSessionTemplate.delete("cn.bdqn.dao.EmployeeMapper.deleteEmployee", employee);
Assert.assertEquals(1, flag);
}
public SqlSessionTemplate getSqlSessionTemplate() {
return sqlSessionTemplate;
} public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
this.sqlSessionTemplate = sqlSessionTemplate;
} }

mybatis-config.xml

<?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>
<typeAliases>
<!--这里给实体类取别名,方便在mapper配置文件中使用-->
<package name="cn.bdqn.pojo"/>
</typeAliases>
</configuration>

applicationContext-mybatis.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- Properties文件读取配置,base的properties -->
<context:property-placeholder location="classpath:jdbc.properties"/> <!-- JNDI获取数据源(使用dbcp连接池) -->
<!-- 因为我们使用的这个数据源是采用 dbcp连接池,对于连接池来说,整个应用中只有一个,
所以作用域需要设置成单例 因为获取数据源是非常消耗性能,所以我们也要采用单例模式-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
scope="singleton">
<property name="driverClassName" value="${driverClassName}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
</bean> <!-- 事务配置 在需要事务管理的地方加@Transactional 注解或者AOP进行事务处理-->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean> <!-- 配置mybitas SqlSessionFactoryBean -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis-config.xml" />
<property name="mapperLocations" value="classpath:cn/bdqn/dao/*.xml"/>
</bean> <!-- 配置SqlSessionTemplate -->
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<!-- spring使用构造的方法进行注入 -->
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean> <!-- 把sqlSessionTemplate注入测试类 -->
<bean id="employeeDaoTest" class="cn.bdqn.test.EmployeeDaoTest">
<property name="sqlSessionTemplate" ref="sqlSessionTemplate"/>
</bean> </beans>

加个日志文件和数据文件就好了。

使用SqlSessionTemplate实现数据库的操作的更多相关文章

  1. php模拟数据库常用操作效果

    test.php <?php header("Content-type:text/html;charset='utf8'"); error_reporting(E_ALL); ...

  2. Android-Sqlite数据库的操作

    Sqlite数据库的简单操作: 设置增删改查的按钮,xml界面布局设置 <?xml version="1.0" encoding="utf-8"?> ...

  3. (四)SQL入门 数据库的操作与事务管理

    数据库的操作,有三个最基本的语句,insert插入,update修改,delete删除. 不同的数据库厂商的实现可能不同,所以就不说具体的语法怎么写的了.说语法也没有意义,到处都可以复制粘贴,记得听某 ...

  4. Laravel框架数据库CURD操作、连贯操作使用方法

    Laravel框架数据库CURD操作.连贯如何来操作了这个操作性是非常的方便简单了我们在这里来为各位介绍一篇相关的教程,具体的细节步骤如下文介绍.   Laravel是一套简洁.优雅的PHP Web开 ...

  5. zabbix数据库mariadb从服务器迁移到云mysql数据库的操作

    zabbix数据库mariadb从本机迁移到云mysql数据库的操作 1.将zabbix数据库导出,并导入到云数据库中 由于数据库较大,如果直接使用shell会话中断会导致数据库导出或者导入失败,使用 ...

  6. 使用my exclipse对数据库进行操作(4)

    四.删除 public class class4 { public static void main(String[] args) { // TODO Auto-generated method st ...

  7. 从C#到Objective-C,循序渐进学习苹果开发(7)--使用FMDB对Sqlite数据库进行操作

    本随笔系列主要介绍从一个Windows平台从事C#开发到Mac平台苹果开发的一系列感想和体验历程,本系列文章是在起步阶段逐步积累的,希望带给大家更好,更真实的转换历程体验.本篇主要开始介绍基于XCod ...

  8. MYSQL数据库的操作

    Mysql的连接方式: 1.原生函数:mysql_connect($server,$username,$password);   //打开一个到Mysql服务器的连接 mysql_select_db( ...

  9. DBA必备:MySQL数据库常用操作和技巧

    DBA必备:MySQL数据库常用操作和技巧 2011-02-25 15:31 kaduo it168 字号:T | T MySQL数据库可以说是DBA们最常见和常用的数据库之一,为了方便大家使用,老M ...

随机推荐

  1. 在c++程序中执行DOS命令

    转自博客:http://blog.csdn.net/ypist/article/details/8485049 #1,system()方式 在C盘根目录下新建文件夹,名称为12: system(&qu ...

  2. js学习第二篇简单语法

    字符串(String)字面量 可以使用单引号或双引号 数组(Array)字面量 定义一个数组: [40, 100, 1, 5, 25, 10] 对象(Object)字面量 定义一个对象: {first ...

  3. [git]图解git常用命令

    本文图解git中最常用的命令.如果你稍微理解git的工作原理,这篇文章能够让你理解的更透彻. 基本用法 约定 命令详解 Diff Commit Checkout Detached HEAD(匿名分支提 ...

  4. 点击事件touches与ios的手势UIGestureRecognizer

    .h文件 @property (weak,nonatomic) IBOutlet UILabel *messageLabel;@property (weak,nonatomic) IBOutlet U ...

  5. 教你写一个Android可快速复用的小键盘输入控件

    引子 在Android项目开发中特别是一些稍大型的项目,面对需求文档的时候你经常会发现很多地方用到了同样的组件,但是又略有不同.比如这个: 右边是一个小键盘输入板,左边当焦点不同的时候分别用右边的小键 ...

  6. Scala之Map,Tuple

    /** * 1,默认情况下Map构造的是不可变的集合,里面的内容不可修改,一旦修改就变成新的Map,原有的Map内容保持不变: * 2,Map的实例是调用工厂方法模式apply来构造Map实例,而需要 ...

  7. 【niubi-job——一个分布式的任务调度框架】----如何开发一个niubi-job的定时任务

    引言 上篇文章LZ主要讲解了niubi-job如何安装,如果看过上一篇文章的话,大家应该知道,niubi-job执行的任务是需要用户自己上传jar包的. 那么问题来了,这个jar包如何产生?有没有要求 ...

  8. Bootstrap系列 -- 23. 图片

    图像在网页制作中也是常要用到的元素,在Bootstrap框架中对于图像的样式风格提供以下几种风格: 1.img-responsive:响应式图片,主要针对于响应式设计 2.img-rounded:圆角 ...

  9. C# JArray与JObject 的使用 json [{}]

    C# JArray与JObject 的使用 STEP1.using Newtonsoft.Json.Linq; STEP2 如何获取json里的某个属性(节点)值,对其删改,新增 //2.1 数组用J ...

  10. MongoDB学习与BUG解答

    简单介绍: MongoDb也是NoSQL中的一种,并且是应用比较火的一门解决高效处理数据的技术. 网上说它是介于关系数据库 和非关系数据库之间的产品,它是非关系数据库中最丰富的,最像关系数据的. Q: ...