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

结构

Student.java

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 + "]";
    }

}

Mybatisf.java

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();

    }

}

jdbc.properties

jdbc.classname =com.mysql.jdbc.Driver
jdbc.url =jdbc:mysql://localhost:3306/sudent
jdbc.username=root
jdbc.password=root

log4j.properties

### 设置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

Student.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="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>

SqlMapConfig.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>

表结构

-- ----------------------------
-- 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);
时间: 2024-11-08 04:22:20

mybatis3.4测试CRUD的相关文章

ElasticSearch(1)CentOS安装ElasticSearch测试CRUD

1,关于ElasticSearch Elasticsearch 是一个实时的分布式搜索和分析引擎.它可以帮助你用前所未有的速度去处理大规模数据. 它可以用于全文搜索,结构化搜索以及分析,当然你也可以将这三者进行组合 维基百科使用 Elasticsearch 来进行全文搜索并高亮显示关键词,以及提供search-as-you-type.did-you-mean等搜索建议功能. 英国卫报使用 Elasticsearch 来处理访客日志,以便能将公众对不同文章的反应实时地反馈给各位编辑. StackO

关于使用mybatis时,或者说是测试CRUD时出现的找不到累的异常讲解

最近在用mybatis做CRUD时,刚把配置文件以及映射文件弄好,才测试能否获取sqlsessionFactory时出现了Caused by: java.lang.ClassNotFoundException: Cannot find class: userMap 网上搜了又搜终于找到了错误,是自己的映射文件里配置问题,查询时返回的是一个集合,那它的参数就是resultMap="" 而我却用alt+/写成了resultType,所以程序一直没正常运行,一直报错... 看到那个resul

SpringMVC3+Mybatis3(登录及CRUD操作)

官网:http://code.google.com/p/mybatis/参考资料 1 兼顾效率,iBatis一些非见用法(10条) http://lavasoft.blog.51cto.com/62575/202886/2 整合Mybatis与Spring3 http://www.iteye.com/topic/1018134一 环境:XP3+Oracle10g+MyEclipse6+(Tomcat or WebLogic92)+JDK1.5 二 工程文件: Spring3+MyIbatis3 

SpringMVC3+Mybatis3(登录及CRUD操作)【转】

一 环境:XP3+Oracle10g+MyEclipse6+(Tomcat or WebLogic92)+JDK1.5 二 工程文件: Spring3+MyIbatis3 工程图片:  jar下载:http://dl.iteye.com/topics/download/d209389e-ecac-390e-85c7-c08849f50348 三 具体代码如下: 1 数据库文件 Java代码   create table USERS ( id       VARCHAR2(50) not null

【学习】整合springboot2.0 和 mybatis,实现基本的CRUD

前言: 本文是在自己整合springboot 2.0 和mybatis时的过程和踩得坑. 先附上github地址:https://github.com/yclxt/springboot-mybatis.git 环境/版本: 工具: Intellij IDEA 2018.3 JDK: 1.8 Springboot: 2.0.4.RELEASE Mybatis : 1.3.2 由于本人是初学者,对druid 和handle Page 不太熟悉,所以只是按照网上的配置加了上去,等了解后会在之后的博客中

尚硅谷-MyBatis的CRUD操作

项目结构: User实体类代码: package com.atguigu.mybatis.bean; public class User { private int id; private String name; private int age; public User() { super(); } public User(int id, String name, int age) { super(); this.id = id; this.name = name; this.age = ag

ORM for Net主流框架汇总与效率测试

框架已经被越来越多的人所关注与使用了,今天我们就来研究一下net方面的几个主流ORM框架,以及它们的效率测试(可能会有遗漏欢迎大家讨论). ORM框架:Object/Relation Mapping(对象/关系 映射)的缩写,易于理解的模型化数据的方法.简单的说就是把数据库的关系型数据类型转换为用对象型程序控制的框架类型. 今天研究的orm框架如下: 1.NHibernate(使用与介绍:http://www.cnblogs.com/stone_w/archive/2011/09/15/2177

02_mybatis的CRUD操作

1.XML 的实现 1.1 定义SQL映射xml文件 <insert id="insertUser" parameterType="cn.imentors.mybatis.test1.User"> insert into users(name, age) values(#{name}, #{age}); </insert> <delete id="deleteUser" parameterType="int

基本的JDBC以及CRUD/Transaction基本操作

先上依赖: <!-- mysql start --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency> <!-- mysql end --> <!-- junit start -->