zoukankan      html  css  js  c++  java
  • mybatis 的 配置文件及简单test

    1... sqlMapperConfig.xml (配置:数据源 和 映射文件 )

     1 <?xml version="1.0" encoding="UTF-8" ?>
     2 <!DOCTYPE configuration
     3         PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
     4         "http://mybatis.org/dtd/mybatis-3-config.dtd">
     5 <configuration>
     6     <environments default="development">
     7         <environment id="development">
     8             <transactionManager type="JDBC" />
     9             <dataSource type="POOLED">
    10                 <property name="driver" value="com.mysql.jdbc.Driver" />
    11                 <property name="url" value="jdbc:mysql://127.0.0.1:3306/studentdb?characterEncoding=utf8" />
    12                 <property name="username" value="root" />
    13                 <property name="password" value="wsc" />
    14             </dataSource>
    15         </environment>
    16     </environments>
    17     <!--映射文件路径-->
    18     <mappers>
    19         <mapper resource="com/wsc/dao/StudentMapper.xml"></mapper>
    20         <!--        <mapper class="com/wsc/Mapper.xml"></mapper>-->
    21     </mappers>
    22 </configuration>
    sqlMapperConfig.xml

     

    2...StudentMapper.xml (配置:SQL语句)

     1 <?xml version="1.0" encoding="utf-8" ?>
     2 <!DOCTYPE mapper
     3         PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
     4         "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
     5 <mapper namespace="Student">
     6     <!--    查询所有-->
     7     <select id="findAll" resultType="Student">
     8         SELECT * FROM student;
     9     </select>
    10     <!--    通过id查询-->
    11     <select id="selectById" resultType="Student" parameterType="java.lang.Integer">
    12         SELECT * FROM student where studentNo=#{studentNo};
    13     </select>
    14     <!--    删除通过id-->
    15     <delete id="drop"  parameterType="java.lang.Integer">
    16        Delete from student where studentNo=#{studentNo};
    17     </delete>
    18     <!--    添加-->
    19     <insert id="add" parameterType="Student">
    20             insert into student values (null,#{studentName},#{age},null,#{classNo});
    21     </insert>
    22     <!--    修改数据-->
    23     <update id="change" parameterType="Student" >
    24         update student set studentName=#{studentName},age=#{age},boreDate=#{boreDate},classNo=#{classNo}  where studentNo=#{studentNo};
    25     </update>
    26     <!--    模糊查询-->
    27     <select id="queryByName" resultType="Student" parameterType="java.lang.String">
    28         select * from student where studentName like "%"#{studentName}"%";
    29     </select>
    30 </mapper>
    StudentMapper.xml

     

    3...pom.xml 配置文件

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <project xmlns="http://maven.apache.org/POM/4.0.0"
     3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     5     <modelVersion>4.0.0</modelVersion>
     6 
     7     <groupId>testmybatis</groupId>
     8     <artifactId>testmybatis04</artifactId>
     9     <version>1.0-SNAPSHOT</version>
    10 
    11 
    12     <properties>
    13         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    14         <maven.compiler.source>1.8</maven.compiler.source>
    15         <maven.compiler.target>1.8</maven.compiler.target>
    16     </properties>
    17     <dependencies>
    18         <!--单元测试-->
    19         <dependency>
    20             <groupId>junit</groupId>
    21             <artifactId>junit</artifactId>
    22             <version>4.12</version>
    23         </dependency>
    24         <!--mysql驱动-->
    25         <dependency>
    26             <groupId>mysql</groupId>
    27             <artifactId>mysql-connector-java</artifactId>
    28             <version>5.1.6</version>
    29         </dependency>
    30         <!--mybatis的依赖-->
    31         <dependency>
    32             <groupId>org.mybatis</groupId>
    33             <artifactId>mybatis</artifactId>
    34             <version>3.4.5</version>
    35         </dependency>
    36          <!--日志资料-->
    37         <dependency>
    38             <groupId>log4j</groupId>
    39             <artifactId>log4j</artifactId>
    40             <version>1.2.17</version>
    41         </dependency>
    42     </dependencies>
    43 </project>
    pom.xml

     

    4...test01

      1 package com.wsc.testMybatis;
      2 
      3 import com.wsc.domain.Student;
      4 import com.wsc.dao.StudentDao;
      5 import com.wsc.dao.daoImpl.StudentDaoImpl;
      6 import org.apache.ibatis.session.SqlSessionFactory;
      7 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
      8 import org.junit.Before;
      9 import org.junit.Test;
     10 
     11 import java.io.InputStream;
     12 
     13 import java.util.Date;
     14 import java.util.List;
     15 
     16 /**
     17  * @version 1.0
     18  * @ClassName TestStudent
     19  * @Description TODO
     20  * @Author WSC
     21  * @Date 2019/6/29 17:01
     22  **/
     23 public class TestStudent {
     24     public SqlSessionFactory sessionFactory=null;
     25     @Before
     26     public void init(){
     27         InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("SqlMapperConfig.xml");
     28         sessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
     29     }
     30     //查询方法
     31     @Test
     32     public void findAll(){
     33         StudentDao studentDao = new StudentDaoImpl(sessionFactory);
     34         List<Student> all = studentDao.findAll();
     35         for(Student list:all){
     36             System.out.println(list);
     37         }
     38     }
     39     /**
     40      * 通过id删除
     41      *
     42      */
     43     @Test
     44     public void drop(){
     45         StudentDao studentDao = new StudentDaoImpl(sessionFactory);
     46         studentDao.drop(6);
     47 
     48     };
     49 
     50     /**
     51      * 通过id 查询
     52      *
     53      */
     54     @Test
     55     public void selectId(){
     56         StudentDao studentDao = new StudentDaoImpl(sessionFactory);
     57         List<Student> students = studentDao.selectId(3);
     58         System.out.println(students);
     59     }
     60 
     61     /**
     62      * 通过对象 添加数据
     63      *
     64      */
     65     @Test
     66     public void add(){
     67         StudentDao studentDao = new StudentDaoImpl(sessionFactory);
     68         Student stu= new Student();
     69 
     70         stu.setStudentName("李国");
     71         stu.setAge(30);
     72 //        Date date = new Date();
     73 //        SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
     74 //        String format = simpleDateFormat.format(date);
     75 
     76         stu.setClassNo(2);
     77         studentDao.add(stu);
     78     }
     79 
     80     /**
     81      * 模糊查询
     82      *
     83      */
     84     @Test
     85     public void selectByName(){
     86         StudentDao studentDao = new StudentDaoImpl(sessionFactory);
     87         List<Student> list = studentDao.selectByName("李");
     88         for(Student stu:list){
     89             System.out.println(stu);
     90         }
     91     }
     92 
     93     /**
     94      * 通过id 修改数据
     95      *
     96      */
     97     @Test
     98     public void change(){
     99         StudentDao studentDao = new StudentDaoImpl(sessionFactory);
    100         Student stu= new Student();
    101         stu.setStudentNo(19);
    102         stu.setStudentName("国21家");
    103         stu.setAge(23);
    104         Date date = new Date();
    105         stu.setBoreDate(date);
    106         stu.setClassNo(1);
    107         studentDao.change(stu);
    108     }
    109 }
    test01

     

    5...test02

      1 package com.wsc.dao;
      2 
      3 import entity.Student;
      4 import org.apache.ibatis.session.SqlSession;
      5 import org.apache.ibatis.session.SqlSessionFactory;
      6 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
      7 import org.junit.Test;
      8 
      9 import java.io.InputStream;
     10 import java.util.Date;
     11 import java.util.List;
     12 
     13 /**
     14  * @version 1.0
     15  * @ClassName StudentTest
     16  * @Description TODO
     17  * @Author WSC
     18  * @Date 2019/6/29 8:42
     19  **/
     20 public class StudentTest {
     21     @Test
     22     public void test(){
     23         // 1...流   加载配置文件 SqlCfig
     24         InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("SqlCfig.xml");
     25       //工厂类 实现创建 工厂类实例对象
     26         SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
     27       //工厂类实例对象调 build 方法 创建
     28         SqlSessionFactory build = sqlSessionFactoryBuilder.build(resourceAsStream);
     29         SqlSession sqlSession = build.openSession();
     30         List<Object> objects = sqlSession.selectList("wsc.findAll");
     31         for(Object obj:objects){
     32             System.out.println(obj);
     33         }
     34         //关闭资源
     35         sqlSession.close();
     36     }
     37     //删除
     38     @Test
     39     public void drop(){
     40         // 1...流   加载配置文件 SqlCfig
     41         InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("SqlCfig.xml");
     42         //工厂类 实现创建 工厂类实例对象
     43         SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
     44         //工厂类实例对象调 build 方法 创建
     45         SqlSessionFactory build = sqlSessionFactoryBuilder.build(resourceAsStream);
     46         SqlSession sqlSession = build.openSession();
     47 
     48 //        Student stu= new Student();
     49 //        stu.setStudentNo(1);
     50         int delete = sqlSession.delete("wsc.drop",1);
     51         System.out.println(delete);
     52         sqlSession.commit();
     53 //        List<Object> objects = sqlSession.selectList("wsc.findAll");
     54 //        for(Object obj:objects){
     55 //            System.out.println(obj);
     56 //        }
     57         //关闭资源
     58         sqlSession.close();
     59     }
     60     //查询by id
     61     @Test
     62     public void selectById(){
     63         // 1...流   加载配置文件 SqlCfig
     64         //1 获取流对象
     65         InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("SqlCfig.xml");
     66         //工厂类 实现创建 工厂类实例对象
     67         // 2获得sqlSessionFactory对象
     68         SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
     69         //工厂类实例对象调 build 方法 创建
     70 
     71         SqlSessionFactory build = sqlSessionFactoryBuilder.build(resourceAsStream);
     72         // 3获得sqlSession对象
     73         SqlSession sqlSession = build.openSession();
     74 
     75         Student student= new Student();
     76          student.setStudentNo(2);
     77         Object o = sqlSession.selectOne("wsc.selectById", student);
     78         System.out.println(o);
     79 
     80 //        List<Object> objects = sqlSession.selectList("wsc.findAll");
     81 //        for(Object obj:objects){
     82 //            System.out.println(obj);
     83 //        }
     84         //关闭资源
     85         sqlSession.close();
     86     }
     87     //添加数据
     88     @Test
     89     public void addById(){
     90         // 1...流   加载配置文件 SqlCfig
     91         InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("SqlCfig.xml");
     92         //工厂类 实现创建 工厂类实例对象
     93         SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
     94         //工厂类实例对象调 build 方法 创建
     95         SqlSessionFactory build = sqlSessionFactoryBuilder.build(resourceAsStream);
     96         SqlSession sqlSession = build.openSession();
     97 
     98         Student stu= new Student();
     99 
    100         stu.setStudentName("李");
    101         stu.setAge(30);
    102 //        Date date = new Date();
    103 //        SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
    104 //        String format = simpleDateFormat.format(date);
    105 
    106         stu.setClassNo(2);
    107 
    108         int insert = sqlSession.insert("wsc.add", stu);
    109         System.out.println(insert);
    110         //手动提交方式
    111         sqlSession.commit();
    112 //        List<Object> objects = sqlSession.selectList("wsc.findAll");
    113 //        for(Object obj:objects){
    114 //            System.out.println(obj);
    115 //        }
    116         //关闭资源
    117         sqlSession.close();
    118     }
    119     //通过姓名模糊查询
    120     @Test
    121     public void queryByName(){
    122         // 1...流   加载配置文件 SqlCfig
    123         InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("SqlCfig.xml");
    124         //工厂类 实现创建 工厂类实例对象
    125         SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
    126         //工厂类实例对象调 build 方法 创建
    127         SqlSessionFactory build = sqlSessionFactoryBuilder.build(resourceAsStream);
    128         SqlSession sqlSession = build.openSession();
    129 
    130 //        Student student= new Student();
    131 //        student.setStudentNo(2);
    132         List<Object> list = sqlSession.selectList("wsc.queryByName", "a");
    133 
    134         for(Object obj:list){
    135             System.out.println(obj);
    136         }
    137 
    138 //        List<Object> objects = sqlSession.selectList("wsc.findAll");
    139 //
    140         //关闭资源
    141         sqlSession.close();
    142     }
    143     //修改数据
    144     @Test
    145     public void change(){
    146         // 1...流   加载配置文件 SqlCfig
    147         InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("SqlCfig.xml");
    148         //工厂类 实现创建 工厂类实例对象
    149         SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
    150         //工厂类实例对象调 build 方法 创建
    151         SqlSessionFactory build = sqlSessionFactoryBuilder.build(resourceAsStream);
    152         SqlSession sqlSession = build.openSession();
    153 
    154         Student stu= new Student();
    155         stu.setStudentNo(3);
    156         stu.setStudentName("国30家");
    157         stu.setAge(20);
    158         Date date = new Date();
    159 //        SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
    160 //        String format = simpleDateFormat.format(date);
    161         stu.setBoreDate(date);
    162         stu.setClassNo(1);
    163 
    164         sqlSession.update("wsc.change", stu);
    165 //        System.out.println(insert);
    166         //手动提交方式
    167         sqlSession.commit();
    168 //        List<Object> objects = sqlSession.selectList("wsc.findAll");
    169 //        for(Object obj:objects){
    170 //            System.out.println(obj);
    171 //        }
    172         //关闭资源
    173         sqlSession.close();
    174     }
    175 }
    test02

     

     

  • 相关阅读:
    可重入函数
    进程间通信的方法和实现
    Qt之Qprocess
    mysql学习(十二)内置函数
    mysql学习(十一)嵌套查询 排序 分组
    mysql学习(十)多表查询
    ubuntu 12.04 安装谷歌浏览器
    mysql学习(九)sql语句
    mysql学习(八)数据表类型-字符集
    mysql远程连接问题-http://www.cnblogs.com/jerome-rong/archive/2013/03/05/2944264.html
  • 原文地址:https://www.cnblogs.com/wangshichang/p/11364295.html
Copyright © 2011-2022 走看看