mybatis3.4测试CRUD
导入包
H:\jar\jdbc\mysql-connector-java-5.1.13-bin.jar
H:\jar\mybatis\mybatis-3.4.1\mybatis-3.4.1.jar
H:\jar\Junit\junit-4.7.jar
结构

package com.wym.model;
import java.util.Date;
public class Student {
private String stuname;
private String stusex;
private Date birthday;
private String stuaddress;
private String stuid;
public String getStuid() {
return stuid;
}
public void setStuid(String stuid) {
this.stuid = stuid;
}
public String getStuname() {
return stuname;
}
public void setStuname(String stuname) {
this.stuname = stuname;
}
public String getStusex() {
return stusex;
}
public void setStusex(String stusex) {
this.stusex = stusex;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getStuaddress() {
return stuaddress;
}
public void setStuaddress(String stuaddress) {
this.stuaddress = stuaddress;
}
@Override
public String toString() {
return "Student [stuid=" + stuid + ", stuname=" + stuname + ", stusex="
+ stusex + ", birthday=" + birthday + ", stuaddress="
+ stuaddress + "]";
}
}
Student
package com.wym.test; import java.io.InputStream;
import java.util.List;
import org.apache.ibatis.io.Resources;
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 com.wym.model.Student; public class Mybatisf { private SqlSessionFactory sqlSessionFactory; @Before
public void getsqlSessionFactory() throws Exception {
String resource = "SqlMapConfig.xml";
InputStream inputStream = null;
inputStream = Resources.getResourceAsStream(resource);
if (sqlSessionFactory == null)
sqlSessionFactory = new SqlSessionFactoryBuilder()
.build(inputStream); } @Test
public void insertstudent() {
SqlSession sqlSession; sqlSession = sqlSessionFactory.openSession();
Student student = new Student();
student.setStuid("wl4811");
student.setStuname("武2"); System.out.println(sqlSession.insert("test.insertstudent", student));
sqlSession.commit(); System.out.println(student.getStuid()); sqlSession.close(); } @Test
public void deletestudentbyid() {
SqlSession sqlSession; sqlSession = sqlSessionFactory.openSession(); System.out.println(sqlSession
.delete("test.deletestudentbyid", "wl1214")); sqlSession.commit(); sqlSession.close(); } @Test
public void updatestudent() {
SqlSession sqlSession; sqlSession = sqlSessionFactory.openSession(); Student student = new Student();
student.setStuid("wl2458");
student.setStuname("吴晏子");
student.setStuaddress("湖北"); System.out.println(sqlSession.update("test.updatestudent", student)); sqlSession.close(); } @Test
public void findstudentbystuid() {
SqlSession sqlSession; sqlSession = sqlSessionFactory.openSession();
Student student = sqlSession.selectOne("test.findstudentbystuid",
"wl1213");
System.out.println(student); sqlSession.close(); } @Test
public void findstudentbystusex() {
SqlSession sqlSession; sqlSession = sqlSessionFactory.openSession();
List<Student> ls = sqlSession.selectList("test.findstudentbystusex",
"M"); System.out.println(ls); sqlSession.close(); } }
Mybatisf
jdbc.properties
jdbc.classname =com.mysql.jdbc.Driver
jdbc.url =jdbc:mysql://localhost:3306/sudent
jdbc.username=root
jdbc.password=root
### 设置Logger输出级别和输出目的地 ###
log4j.rootLogger=debug,stdout,logfile ### 把日志信息输出到控制台 ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
#log4j.appender.stdout.Target=System.err
log4j.appender.stdout.layout=org.apache.log4j.SimpleLayout ### 把日志信息输出到文件:jbit.log ###
log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=jbit.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %F %p %m%n ###显示SQL语句部分
log4j.logger.com.ibatis=DEBUG
log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=DEBUG
log4j.logger.com.ibatis.common.jdbc.ScriptRunner=DEBUG
log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
log4j.properties
<?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="test">
<select id="insertstudent">
insert into student(stuid,stuname,stusex,birthday,stuaddress ) VALUES(#{stuid},#{stuname},#{stusex},#{birthday},#{stuaddress})
</select> <select id="deletestudentbyid" parameterType="com.wym.model.Student" resultType="int" >
delete from student where stuid=#{stuid}
</select> <select id="updatestudent" >
update student set stuname=#{stuname},stusex=#{stusex},birthday=#{birthday},stuaddress=#{stuaddress} where stuid=#{stuid}
</select> <select id="findstudentbystuid" parameterType="String" resultType="com.wym.model.Student" >
select * from student where stuid=#{stuid}
</select> <select id="findstudentbystusex" parameterType="String" resultType="com.wym.model.Student" >
select * from student where stusex=#{stusex}
</select> </mapper>
student.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>
<properties resource="jdbc.properties"> </properties> <settings>
<setting name="logImpl" value="LOG4J"/>
</settings> <environments default="development">
<environment id="development"> <transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${jdbc.classname}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments> <mappers> <mapper resource="sqlmap/Student.xml"/> </mappers> </configuration>
SqlMapConfig.xml
-- ----------------------------
-- Table structure for student
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`stuid` varchar(10) NOT NULL COMMENT '学号',
`stuname` varchar(20) DEFAULT NULL COMMENT '姓名',
`stusex` char(1) DEFAULT NULL COMMENT '性别',
`birthday` date DEFAULT NULL COMMENT '生日',
`stuaddress` varchar(30) DEFAULT NULL COMMENT '住址',
PRIMARY KEY (`stuid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `student` VALUES ('wl1213', '茂', 'M', '1991-10-05', '湖南');
INSERT INTO `student` VALUES ('wl1216', '元强', 'M', '1991-10-07', '湖南');
INSERT INTO `student` VALUES ('wl2458', '晏子', null, null, '湖北');
INSERT INTO `student` VALUES ('wl2811', '张山', null, null, null);
INSERT INTO `student` VALUES ('wl4811', '李武', null, null, null);
INSERT INTO `student` VALUES ('wl9811', '默默', null, null, null);
db.sql
mybatis3.4测试CRUD的更多相关文章
- ORM for Net主流框架汇总与效率测试
框架已经被越来越多的人所关注与使用了,今天我们就来研究一下net方面的几个主流ORM框架,以及它们的效率测试(可能会有遗漏欢迎大家讨论). ORM框架:Object/Relation Mapping( ...
- 【MyBatis】MyBatis实现CRUD操作
1.实现基本CRUD功能 使用MyBatis对数据完整的操作,也就是CRUD功能的实现.根据之前的内容,要想实现CRUD,只需要进行映射文件的配置. 范例:修改EmpMapper.xml文件,实现CR ...
- 1 JPA入门----项目搭建以及CRUD
maven搭建JPA开发环境 1 依赖的maven pom文件 主要有hibernate-core.hibernate-entitymanager.javax-persistence.mysq ...
- 【经验分享】Mongodb操作类实现CRUD
一.背景 公司项目中在做数据存储时使用到Mongodb,所以想着将Mongodb的操作封装后可供项目中其他成员方便使用. 附上Mongodb的下载地址: 下载 1.Mongodb类 此类主要是用来构造 ...
- MyBatis基础-CRUD
一.mybatis 环境搭建步骤 第一步:创建 maven 工程第二步:导入坐标第三步:编写必要代码(实体类和持久层接口)第四步:编写 SqlMapConfig.xml第五步:编写映射配置文件第六步 ...
- mybatis 学习视频总结记录
学习mybaits简单增删改查例子记录 此整理是学习视频后的视频内容整理,后半段还没有整理 网易云课堂 SSM高级整合视频 地址 : http://study.163.com/course/cours ...
- 13-MyBatis03(逆向工程)
MyBatis逆向工程 1.导入jar包 <dependency> <groupId>org.mybatis</groupId> <artifactId> ...
- Net环境下比较流行的ORM框架对比
个人感觉在Java领域大型开发都离不了ORM的身影,所谓的SSH就是Spring+Struts+Hibernate,除了在学习基础知识的时候被告知可以使用JDBC操作数据库之外,大量的书籍中都是讲述使 ...
- [译]Testing Node.js With Mocha and Chai
原文: http://mherman.org/blog/2015/09/10/testing-node-js-with-mocha-and-chai/#.ViO8oBArIlJ 为什么要测试? 在此之 ...
随机推荐
- MVC提交时验证
第一种 @using (Html.BeginForm("ProdPromotionEdit", "Product", FormMethod.Post, new ...
- 【高德地图API】从零开始学高德JS API(四)搜索服务——POI搜索|自动完成|输入提示|行政区域|交叉路口|自有数据检索
原文:[高德地图API]从零开始学高德JS API(四)搜索服务——POI搜索|自动完成|输入提示|行政区域|交叉路口|自有数据检索 摘要:地图服务,大家能想到哪些?POI搜素,输入提示,地址解析,公 ...
- Java数据结构与算法(1) - ch02有序表(OrderedArray)
有序表需要掌握的插入方法,删除方法和二分法查找方法. 插入方法: 从前往后找到比要插入的值大的数组项,将该数组项及之后的项均后移一位(从最后一项起依次后移),最后将要插入的值插入当前数组项. 删除方法 ...
- 我在Github上的flare-spark项目
Flare-Spark 介绍 我在自己的github上建了个flare-spark项目,本身是Apache Spark项目Master分支的镜像.在Spark的基础上,添加了flare子项目. 估计大 ...
- 经验总结35--IP地址区域匹配
想知道客服端訪问的IP地址是多少,并知道区域. 一般能够去http://www.ip138.com/,输入IP查询,但没提供比較好的接口,程序使用不方便. 另外有些企业提供一些离线的IP数据库,能够进 ...
- Object-C 基础学习笔记(for,foreach,while,switch)
int main(int argc, const char * argv[]) { //for,foreach,while,do-while,switch NSMutableArray* mutabl ...
- Spring之使用Annotation注解开发项目
我们也可以使用Annotation来实现注入操作,提高我们写代码的灵活性和效率.spring中要使用annotation,需要在配置文件中增加: <beans xmlns="http: ...
- MySQL 一般查询日志(General Query Log)
与大多数关系型数据库,日志文件是MySQL数据库的一个重要组成部分.MySQL有几种不同的日志文件,通常包括错误日志文件,二进制日志,通用日志.慢查询日志,等等. 这些日志能够帮助我们定位mysqld ...
- c# 委托详解
1.委托声明 2.委托入门实例 namespace ConsoleApplication1 { public delegate void methodDelegate(string str); cla ...
- openwrt固件支持3G和4G上网卡
http://wiki.openwrt.org/doc/howtobuild/wireless-router-with-a-3g-dongle Building image with support ...