数据:
Student{id int,name String ,age int}
配置mybatis-config.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. <!-- <properties resource="jdbc.properties"/> -->
  7. <properties>
  8. <property name="jdbc.driverClassName" value="com.mysql.jdbc.Driver"/>
  9. <property name="jdbc.url" value="jdbc:mysql://localhost:3306/db_mybatis"/>
  10. <property name="jdbc.username" value="root"/>
  11. <property name="jdbc.password" value="123456"/>
  12. </properties>
  13. <!-- <typeAliases>
  14. <typeAlias alias="Student" type="com.qinb.model.Student"/>
  15. </typeAliases> -->
  16. <typeAliases>
  17. <package name="com.qinb.model"/>
  18. </typeAliases>
  19. <environments default="development">
  20. <environment id="development">
  21. <transactionManager type="JDBC" />
  22. <dataSource type="POOLED">
  23. <property name="driver" value="${jdbc.driverClassName}" />
  24. <property name="url" value="${jdbc.url}" />
  25. <property name="username" value="${jdbc.username}" />
  26. <property name="password" value="${jdbc.password}" />
  27. </dataSource>
  28. </environment>
  29. <environment id="test">
  30. <transactionManager type="JDBC" />
  31. <dataSource type="POOLED">
  32. <property name="driver" value="${jdbc.driverClassName}" />
  33. <property name="url" value="${jdbc.url}" />
  34. <property name="username" value="${jdbc.username}" />
  35. <property name="password" value="${jdbc.password}" />
  36. </dataSource>
  37. </environment>
  38. </environments>
  39. <mappers>
  40. <!-- <mapper resource="com/qinb/mappers/StudentMapper.xml" /> -->
  41. <!-- <mapper class="com.qinb.mappers.StudentMapper"/> -->
  42. <package name="com.qinb.mappers"/>
  43. </mappers>
  44. </configuration>
工厂:
  1. package com.qinb.util;
  2. import java.io.InputStream;
  3. import org.apache.ibatis.io.Resources;
  4. import org.apache.ibatis.session.SqlSession;
  5. import org.apache.ibatis.session.SqlSessionFactory;
  6. import org.apache.ibatis.session.SqlSessionFactoryBuilder;
  7. public class SqlSessionFactoryUtil {
  8. private static SqlSessionFactory sqlSessionFactory;
  9. public static SqlSessionFactory getSqlSessionFactory(){
  10. if(sqlSessionFactory==null){
  11. InputStream inputStream=null;
  12. try{
  13. inputStream=Resources.getResourceAsStream("mybatis-config.xml");
  14. sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
  15. }catch(Exception e){
  16. e.printStackTrace();
  17. }
  18. }
  19. return sqlSessionFactory;
  20. }
  21. public static SqlSession openSession(){
  22. return getSqlSessionFactory().openSession();
  23. }
  24. }


Dao接口:
  1. package com.qinb.mappers;
  2. import java.util.List;
  3. import com.qinb.model.Student;
  4. public interface StudentMapper {
  5. public int add(Student student);
  6. public int update(Student student);
  7. public int delete(Integer id);
  8. public Student findById(Integer id);
  9. public List<Student> list();
  10. }
Mapper.xml实现:
  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="com.qinb.mappers.StudentMapper">
  6. <resultMap type="Student" id="StudentResult">
  7. <id property="id" column="id"/>
  8. <result property="name" column="name"/>
  9. <result property="age" column="age"/>
  10. </resultMap>
  11. <insert id="add" parameterType="Student" >
  12. insert into t_student values(null,#{name},#{age})
  13. </insert>
  14. <update id="update" parameterType="Student" >
  15. update t_student set name=#{name},age=#{age} where id=#{id}
  16. </update>
  17. <delete id="delete" parameterType="Integer">
  18. delete from t_student where id=#{id}
  19. </delete>
  20. <select id="findById" parameterType="Integer" resultType="Student">
  21. select * from t_student where id=#{id}
  22. </select>
  23. <select id="list" resultMap="StudentResult">
  24. select * from t_student
  25. </select>
  26. </mapper>
测试类:
  1. package com.qinb.service;
  2. import static org.junit.Assert.fail;
  3. import java.util.List;
  4. import java.util.logging.Logger;
  5. import org.apache.ibatis.session.SqlSession;
  6. import org.junit.After;
  7. import org.junit.Before;
  8. import org.junit.Test;
  9. import com.qinb.mappers.StudentMapper;
  10. import com.qinb.model.Student;
  11. import com.qinb.util.SqlSessionFactoryUtil;
  12. public class StudentTest2 {
  13. private static Logger logger=Logger.getLogger(StudentTest2.class.getName());
  14. private SqlSession sqlSession=null;
  15. private StudentMapper studentMapper= null;
  16. @Before
  17. public void setUp() throws Exception {
  18. sqlSession = SqlSessionFactoryUtil.openSession();
  19. studentMapper=sqlSession.getMapper(StudentMapper.class);
  20. }
  21. @After
  22. public void tearDown() throws Exception {
  23. sqlSession.close();
  24. }
  25. @Test
  26. public void testAdd(){
  27. logger.info("添加学生");
  28. Student student = new Student("王五",20);
  29. studentMapper.add(student);
  30. sqlSession.commit();
  31. }
  32. @Test
  33. public void testUpdate(){
  34. logger.info("更新学生");
  35. Student student = new Student(5,"秦豹2",22);
  36. studentMapper.update(student);
  37. sqlSession.commit();
  38. }
  39. @Test
  40. public void testDelete(){
  41. logger.info("删除学生");
  42. studentMapper.delete(2);
  43. sqlSession.commit();
  44. }
  45. @Test
  46. public void testFindById(){
  47. logger.info("根据id获取学生");
  48. Student student =studentMapper.findById(5);
  49. System.out.println(student);
  50. sqlSession.commit();
  51. //查询可以不用提交事物
  52. }
  53. @Test
  54. public void testList(){
  55. logger.info("获取所有学生");
  56. List<Student> studentList = studentMapper.list();
  57. for(Student stu:studentList){
  58. System.out.println(stu);
  59. }
  60. }
  61. @Test
  62. public void test() {
  63. fail("Not yet implemented");
  64. }
  65. }
项目:
 

Mybatis数据的增删改查的更多相关文章

  1. Mybatis框架基于注解的方式,实对数据现增删改查

    编写Mybatis代码,与spring不一样,不需要导入插件,只需导入架包即可: 在lib下 导入mybatis架包:mybatis-3.1.1.jarmysql驱动架包:mysql-connecto ...

  2. Mybatis学习总结(二)—使用接口实现数据的增删改查

    在这一篇中,让我们使用接口来实现一个用户数据的增删改查. 完成后的项目结构如下图所示: 在这里,person代表了一个用户的实体类.在该类中,描述了相关的信息,包括id.name.age.id_num ...

  3. Mybatis实现数据的增删改查

    Mybatis实现数据的增删改查 1.项目结构(使用maven创建项目) 2.App.java package com.GetcharZp.MyBatisStudy; import java.io.I ...

  4. dbutils中实现数据的增删改查的方法,反射常用的方法,绝对路径的写法(杂记)

    jsp的三个指令为:page,include,taglib... 建立一个jsp文件,建立起绝对路径,使用时,其他jsp文件导入即可 导入方法:<%@ include file="/c ...

  5. MVC模式:实现数据库中数据的增删改查功能

    *.数据库连接池c3p0,连接mysql数据库: *.Jquery使用,删除时跳出框,确定是否要删除: *.使用EL和JSTL,简化在jsp页面中插入的java语言 1.连接数据库 (1)导入连接数据 ...

  6. Hibernate3回顾-5-简单介绍Hibernate session对数据的增删改查

    5. Hibernate对数据的增删改查 5.1Hibernate加载数据 两种:get().load() 一. Session.get(Class arg0, Serializable arg1)方 ...

  7. MyBatis简单的增删改查以及简单的分页查询实现

    MyBatis简单的增删改查以及简单的分页查询实现 <? xml version="1.0" encoding="UTF-8"? > <!DO ...

  8. 数据的增删改查(三层)<!--待补充-->

    进行数据操作必然少了对数据的增删改查,用代码生成器生成的代码不是那么满意!方便在今后使用,这里就主要写“数据访问层(Dal)” 既然这里提到三层架构:有必要将三层内容在这里详细介绍一下(待补充) 注: ...

  9. vue实现对表格数据的增删改查

    在管理员的一些后台页面里,个人中心里的数据列表里,都会有对这些数据进行增删改查的操作.比如在管理员后台的用户列表里,我们可以录入新用户的信息,也可以对既有的用户信息进行修改.在vue中,我们更应该专注 ...

随机推荐

  1. js生成中文二维码

    http://www.cnblogs.com/xcsn/archive/2013/08/14/3258035.html http://www.jb51.net/article/64928.htm 使用 ...

  2. 初次使用VCS仿真软件

    由于刚开始接触VCS,对于VCS不是太了解,在网上找了很多的资料终于遇到了一个相对比较初级的入门资料,这个资料是以一个简单的4位加法器为例来介绍vcs的用法的,比较好入门,这个文章的地址如下: htt ...

  3. C++面向对象高级编程(八)模板

    技术在于交流.沟通,转载请注明出处并保持作品的完整性. 这节课主要讲模板的使用,之前我们谈到过函数模板与类模板 (C++面向对象高级编程(四)基础篇)这里不再说明 1.成员模板 成员模板:参数为tem ...

  4. Python的介绍及Pycharm软件的安装

    一.Python介绍 1.  Python是一种解释性.面向对象.动态数据类型的高级程序设计语言. Python语言创始人是吉多.范罗苏姆:起源与1989年 2.  缺点:运行速度慢(由于是解释性语言 ...

  5. React 与 可视化

    一般会想到 canvas 和 svg ; svg更适合画图, 但由于cavans在移动端的良好兼容性, 使用的更广; 什么是svg, scalable vector graphics  全称 可缩放矢 ...

  6. Visual studio 生成后事件说明

      在“配置属性->生成事件->生成后事件”属性页中的“命令行”编辑框中输入如下命令: copy "$(ProjectDir)$(IntDir)\$(ProjectName).t ...

  7. [Scala]Scala学习笔记五 Object

    1. 单例对象 Scala没有静态方法或静态字段,可以使用object来达到这个目的,对象定义了某个类的单个实例: object Account{ private var lastNumber = 0 ...

  8. React-Native基础_5.列表视图ListView

    列表视图ListView 用来显示垂直滚动列表,需要指定两个东西,1 数据的来源 dataSource,2 渲染列表的条目布局 rendRow 'use strict' import React, { ...

  9. keras系列︱keras是如何指定显卡且限制显存用量

    keras在使用GPU的时候有个特点,就是默认全部占满显存. 若单核GPU也无所谓,若是服务器GPU较多,性能较好,全部占满就太浪费了. 于是乎有以下三种情况: - 1.指定GPU - 2.使用固定显 ...

  10. SQL基础四(例子)

    ------------------------------------------------ --分别创建student/course/score表 Create table student ( ...