首页 > 代码库 > mybatis3.4测试CRUD

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

mybatis3.4测试CRUD