Mybatis数据的增删改查
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration><!-- <properties resource="jdbc.properties"/> --><properties><property name="jdbc.driverClassName" value="com.mysql.jdbc.Driver"/><property name="jdbc.url" value="jdbc:mysql://localhost:3306/db_mybatis"/><property name="jdbc.username" value="root"/><property name="jdbc.password" value="123456"/></properties><!-- <typeAliases><typeAlias alias="Student" type="com.qinb.model.Student"/></typeAliases> --><typeAliases><package name="com.qinb.model"/></typeAliases><environments default="development"><environment id="development"><transactionManager type="JDBC" /><dataSource type="POOLED"><property name="driver" value="${jdbc.driverClassName}" /><property name="url" value="${jdbc.url}" /><property name="username" value="${jdbc.username}" /><property name="password" value="${jdbc.password}" /></dataSource></environment><environment id="test"><transactionManager type="JDBC" /><dataSource type="POOLED"><property name="driver" value="${jdbc.driverClassName}" /><property name="url" value="${jdbc.url}" /><property name="username" value="${jdbc.username}" /><property name="password" value="${jdbc.password}" /></dataSource></environment></environments><mappers><!-- <mapper resource="com/qinb/mappers/StudentMapper.xml" /> --><!-- <mapper class="com.qinb.mappers.StudentMapper"/> --><package name="com.qinb.mappers"/></mappers></configuration>
package com.qinb.util;import java.io.InputStream;import org.apache.ibatis.io.Resources;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;public class SqlSessionFactoryUtil {private static SqlSessionFactory sqlSessionFactory;public static SqlSessionFactory getSqlSessionFactory(){if(sqlSessionFactory==null){InputStream inputStream=null;try{inputStream=Resources.getResourceAsStream("mybatis-config.xml");sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);}catch(Exception e){e.printStackTrace();}}return sqlSessionFactory;}public static SqlSession openSession(){return getSqlSessionFactory().openSession();}}
package com.qinb.mappers;import java.util.List;import com.qinb.model.Student;public interface StudentMapper {public int add(Student student);public int update(Student student);public int delete(Integer id);public Student findById(Integer id);public List<Student> list();}
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.qinb.mappers.StudentMapper"><resultMap type="Student" id="StudentResult"><id property="id" column="id"/><result property="name" column="name"/><result property="age" column="age"/></resultMap><insert id="add" parameterType="Student" >insert into t_student values(null,#{name},#{age})</insert><update id="update" parameterType="Student" >update t_student set name=#{name},age=#{age} where id=#{id}</update><delete id="delete" parameterType="Integer">delete from t_student where id=#{id}</delete><select id="findById" parameterType="Integer" resultType="Student">select * from t_student where id=#{id}</select><select id="list" resultMap="StudentResult">select * from t_student</select></mapper>
package com.qinb.service;import static org.junit.Assert.fail;import java.util.List;import java.util.logging.Logger;import org.apache.ibatis.session.SqlSession;import org.junit.After;import org.junit.Before;import org.junit.Test;import com.qinb.mappers.StudentMapper;import com.qinb.model.Student;import com.qinb.util.SqlSessionFactoryUtil;public class StudentTest2 {private static Logger logger=Logger.getLogger(StudentTest2.class.getName());private SqlSession sqlSession=null;private StudentMapper studentMapper= null;@Beforepublic void setUp() throws Exception {sqlSession = SqlSessionFactoryUtil.openSession();studentMapper=sqlSession.getMapper(StudentMapper.class);}@Afterpublic void tearDown() throws Exception {sqlSession.close();}@Testpublic void testAdd(){logger.info("添加学生");Student student = new Student("王五",20);studentMapper.add(student);sqlSession.commit();}@Testpublic void testUpdate(){logger.info("更新学生");Student student = new Student(5,"秦豹2",22);studentMapper.update(student);sqlSession.commit();}@Testpublic void testDelete(){logger.info("删除学生");studentMapper.delete(2);sqlSession.commit();}@Testpublic void testFindById(){logger.info("根据id获取学生");Student student =studentMapper.findById(5);System.out.println(student);sqlSession.commit();//查询可以不用提交事物}@Testpublic void testList(){logger.info("获取所有学生");List<Student> studentList = studentMapper.list();for(Student stu:studentList){System.out.println(stu);}}@Testpublic void test() {fail("Not yet implemented");}}
Mybatis数据的增删改查的更多相关文章
- Mybatis框架基于注解的方式,实对数据现增删改查
编写Mybatis代码,与spring不一样,不需要导入插件,只需导入架包即可: 在lib下 导入mybatis架包:mybatis-3.1.1.jarmysql驱动架包:mysql-connecto ...
- Mybatis学习总结(二)—使用接口实现数据的增删改查
在这一篇中,让我们使用接口来实现一个用户数据的增删改查. 完成后的项目结构如下图所示: 在这里,person代表了一个用户的实体类.在该类中,描述了相关的信息,包括id.name.age.id_num ...
- Mybatis实现数据的增删改查
Mybatis实现数据的增删改查 1.项目结构(使用maven创建项目) 2.App.java package com.GetcharZp.MyBatisStudy; import java.io.I ...
- dbutils中实现数据的增删改查的方法,反射常用的方法,绝对路径的写法(杂记)
jsp的三个指令为:page,include,taglib... 建立一个jsp文件,建立起绝对路径,使用时,其他jsp文件导入即可 导入方法:<%@ include file="/c ...
- MVC模式:实现数据库中数据的增删改查功能
*.数据库连接池c3p0,连接mysql数据库: *.Jquery使用,删除时跳出框,确定是否要删除: *.使用EL和JSTL,简化在jsp页面中插入的java语言 1.连接数据库 (1)导入连接数据 ...
- Hibernate3回顾-5-简单介绍Hibernate session对数据的增删改查
5. Hibernate对数据的增删改查 5.1Hibernate加载数据 两种:get().load() 一. Session.get(Class arg0, Serializable arg1)方 ...
- MyBatis简单的增删改查以及简单的分页查询实现
MyBatis简单的增删改查以及简单的分页查询实现 <? xml version="1.0" encoding="UTF-8"? > <!DO ...
- 数据的增删改查(三层)<!--待补充-->
进行数据操作必然少了对数据的增删改查,用代码生成器生成的代码不是那么满意!方便在今后使用,这里就主要写“数据访问层(Dal)” 既然这里提到三层架构:有必要将三层内容在这里详细介绍一下(待补充) 注: ...
- vue实现对表格数据的增删改查
在管理员的一些后台页面里,个人中心里的数据列表里,都会有对这些数据进行增删改查的操作.比如在管理员后台的用户列表里,我们可以录入新用户的信息,也可以对既有的用户信息进行修改.在vue中,我们更应该专注 ...
随机推荐
- webmin 安装
webmin 安装1.下载:wget http://prdownloads.sourceforge.net/webadmin/webmin-1.850-1.noarch.rpm2.安装依赖环境:yum ...
- rsync配置文件的参数详解
rsyncd.conf配置文件常用参数说明: rsyncd.conf参数 参数说明 uid=rsync rsync使用的用户. gid=rsync rsync使用的用户组(用户所在的组) use ch ...
- Django-自定义分页组件
1.封装的分页代码: class PageInfo(object): def __init__(self,current_page,all_count,per_page,base_url,show_p ...
- Java基础学习-内部类
/*内部类: 成员内部类 局部内部类 匿名内部类*/ package insideclass; /*成员内部类: * 在类的成员位置,和成员变量,成员方法的位置是一样的. * 内部类可以直接访问为外部 ...
- CDN加速的实现 --- varnish
一.什么是CDN cdn全称为内容分发网络(Content Delivery Network).基本思想是尽可能避开互联网上有可能影响数据传输速度和稳定性的瓶颈和环节,是内容传输地更快.更稳定.通过在 ...
- 【dlbook】正则化
对学习算法的修改——旨在减少泛化误差而不是训练误差 显著减少方差而不过度增加偏差. [参数范数惩罚] 通常只对权重做惩罚而不对偏置做惩罚,原因是拟合偏置比拟合权重容易很多. 不同层使用不同惩罚的代价很 ...
- Hibernate常见配置详细解释
<!--标准的XML文件的起始行,version='1.0'表明XML的版本,encoding='gb2312'表明XML文件的编码方式--> <?xml version='1. ...
- zookeeper的c API 单线程与多线程问题 cli_st和cli_mt
同样的程序,在centos和ubuntu上都没有问题,在solaris上问题却多多,据说是solaris管理更加严格. zookeeper_init方法,在传入一个错误的host也能初始化出一个非空的 ...
- openoffice在连接时有错误,无法连接上
openoffice在连接时有错误,无法连接上 报如下错误: java.net.ConnectException: connection failed: socket,host=localhost,p ...
- UNIX环境高级编程 文件I/O
大多数文件I/O 只需要用到 5个函数 : open , read , write , lseek , close 本章描述的都是不带缓冲的I/O(read write 都调用内核中的一个系统调 ...