使用SqlSessionTemplate实现数据库的操作

时间:2022-12-15 18:15:25
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对数据库进行操作&lpar;4&rpar;

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

  7. 从C&num;到Objective-C,循序渐进学习苹果开发&lpar;7&rpar;--使用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. Ruby-递归和尾递归

    递归和迭代的区别 递归: 1)递归就是在过程或函数里面调用自身; 2)在使用递归时,必须有一个明确的递归结束条件,称为递归出口. 迭代: 利用变量的原值推算出变量的一个新值.如果递归是自己调用自己的话 ...

  2. 【Lamp】 Linux 下安装PHP&plus;Apache&plus;Mysql 手记

    [0]写在最前 由于准备实习原因,今天又重温了Lamp的搭建过程,之前一直是看燕十八老师2012年的教程学习,因此今天也是拿了十八哥的lamp搭建笔记作参考.但这次按照笔记重新搭建,发现了很多问题,由 ...

  3. 记一次动画的优化--requestAnimationFrame、webp

    需要写一个类似帧动画的东西,但是每一帧是一张全屏的图,而且量特别大,600都张,而且存在跳帧的问题,只有把速度调的很快还可以看着不跳帧.但是只用谷歌还真正常播放. 其实优化起来两个方面.一个是用req ...

  4. hdoj 5443 The Water Problem【线段树求区间最大值】

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5443 刷道水题助助兴 #include<stdio.h> #include<stri ...

  5. python 基本模块

    time & datetime模块 import timeimport datetime# ## # print("ss")# #time.sleep(5)# # prin ...

  6. 那些年&comma;让我们一起着迷的Spring

    构建企业级应用框架(SpringMVC+Spring+Hibernate/ibatis[Mybatis]) 框架特点:半成品,封装了特定的处理流程和控制逻辑,成熟的,不断升级的软件.重用度高,开发效率 ...

  7. 面试技巧,如何通过索引说数据库优化能力&comma;内容来自Java web轻量级开发面试教程

    上星期写了一个篇文章,数据库方面的面试技巧,如何从建表方面展示自己能力,承蒙管理员抬举,放入首页,也承蒙各位厚爱,两天内收获了将近770个点击,也一度进入48小时热榜. 为了感谢管理员和大家的支持,再 ...

  8. Day Four

    站立式会议 站立式会议内容总结 442 今天:整合主页两个部分的逻辑代码,主页及其跳转基本完成 遇到的问题:无 明天:阅读图书界面逻辑部分完成 331 今天:学习java反射添加类数据到数据库 遇到问 ...

  9. java去除字符串的html标签

    //方法一 public String stripHtml(String content) { // <p>段落替换为换行 content = content.replaceAll(&qu ...

  10. 翻译&colon; 星球生成 II

    翻译: 星球生成 II 本文翻译自Planet Generation - Part II 译者: FreeBlues 以下为译文: 概述 在前一章 我解释了如何为星球创建一个几何球体. 在本文中, 我 ...