使用SqlSessionTemplate实现数据库的操作
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实现数据库的操作的更多相关文章
- php模拟数据库常用操作效果
test.php <?php header("Content-type:text/html;charset='utf8'"); error_reporting(E_ALL); ...
- Android-Sqlite数据库的操作
Sqlite数据库的简单操作: 设置增删改查的按钮,xml界面布局设置 <?xml version="1.0" encoding="utf-8"?> ...
- (四)SQL入门 数据库的操作与事务管理
数据库的操作,有三个最基本的语句,insert插入,update修改,delete删除. 不同的数据库厂商的实现可能不同,所以就不说具体的语法怎么写的了.说语法也没有意义,到处都可以复制粘贴,记得听某 ...
- Laravel框架数据库CURD操作、连贯操作使用方法
Laravel框架数据库CURD操作.连贯如何来操作了这个操作性是非常的方便简单了我们在这里来为各位介绍一篇相关的教程,具体的细节步骤如下文介绍. Laravel是一套简洁.优雅的PHP Web开 ...
- zabbix数据库mariadb从服务器迁移到云mysql数据库的操作
zabbix数据库mariadb从本机迁移到云mysql数据库的操作 1.将zabbix数据库导出,并导入到云数据库中 由于数据库较大,如果直接使用shell会话中断会导致数据库导出或者导入失败,使用 ...
- 使用my exclipse对数据库进行操作(4)
四.删除 public class class4 { public static void main(String[] args) { // TODO Auto-generated method st ...
- 从C#到Objective-C,循序渐进学习苹果开发(7)--使用FMDB对Sqlite数据库进行操作
本随笔系列主要介绍从一个Windows平台从事C#开发到Mac平台苹果开发的一系列感想和体验历程,本系列文章是在起步阶段逐步积累的,希望带给大家更好,更真实的转换历程体验.本篇主要开始介绍基于XCod ...
- MYSQL数据库的操作
Mysql的连接方式: 1.原生函数:mysql_connect($server,$username,$password); //打开一个到Mysql服务器的连接 mysql_select_db( ...
- DBA必备:MySQL数据库常用操作和技巧
DBA必备:MySQL数据库常用操作和技巧 2011-02-25 15:31 kaduo it168 字号:T | T MySQL数据库可以说是DBA们最常见和常用的数据库之一,为了方便大家使用,老M ...
随机推荐
- android 6.0(api 23) SDK,不再提供org.apache.http.*(只保留几个类)
在使用android-async-http的时候我的apl 更新到了23,我的build version也是23的时候出现了,org.apache.http.Header这个类找不到的情况,原因是在a ...
- Xcode7 项目转 Xcode6 时 出现问题
target specifies product type 'com.apple.product-type.bundle.ui-testing', but there's no such produc ...
- Scala之Map,Tuple
/** * 1,默认情况下Map构造的是不可变的集合,里面的内容不可修改,一旦修改就变成新的Map,原有的Map内容保持不变: * 2,Map的实例是调用工厂方法模式apply来构造Map实例,而需要 ...
- [wikioi 1034][CTSC 1999]家园(网络流)
由于人类对自然的疯狂破坏,人们意识到在大约2300年之后,地球不能再居住了,于是在月球上建立了新的绿地,以便在需要时移民.令人意想不到的是,2177年冬由于未知的原因,地球环境发生了连锁崩溃,人类必须 ...
- FileStream大文件复制
FileStream缓冲读取和写入可以提高性能.FileStream读取文件的时候,是先讲流放入内存,经Flash()方法后将内存中(缓冲中)的数据写入文件.如果文件非常大,势必消耗性能.特封装在Fi ...
- 如何引用XML文件生成C#类
目录 XSD File Generate Class File Simply. 1 Why use XSD file to create C# classes?... 2 How to convert ...
- iOS彩票项目--第七天,初次读取json数据、KVC转模型技巧、运行时字典转模型以及初步对显示网页的操作并且跟踪标签
一.初次读取json数据 二.KVC转模型技巧,这里的技巧主要解决的是字典中的key 与 模型中有的属性对应不起来的时候 的解决办法 <方法1> <方法2>运行时字典转模型,运 ...
- 【OpenJudge1814】 恼人的青蛙 暴力+剪枝优化
此题poj1054上也有 #include<cstdio> #include<cstring> #include<algorithm> using namespac ...
- JS模式:jq中简单的模式--》采摘自js设计(tomxu_version)
<!DOCTYPE html> <html> <head> <title></title> </head> <body&g ...
- SP*
1.PS1——默认提示符 root@tcx2250-14:/etc# echo $PS1\u@\h:\w\$ \u是用户名 \h是主机名 \w是当前目录的完整路径.请注意当你在主目录下的时候,如上面所 ...