首页 > 学院 > 开发设计 > 正文

MyBatis 菜鸟教程 2 单表操作

2019-11-14 12:12:16
字体:
来源:转载
供稿:网友

对单表进行CRUD操作

模型对象User

package com.jege.mybatis.mapper;/** * @author JE哥 * @email 1272434821@QQ.com * @description:单表 */public class User { PRivate Long id; private String name; private Integer age; public User() { } public User(String name, Integer age) { this.name = name; this.age = age; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "User [id=" + id + ", name=" + name + ", age=" + age + "]"; }}

映射文件UserMapper.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="com.jege.mybatis.mapper.UserMapper"> <!-- 删表 --> <update id="dropTable"> DROP TABLE IF EXISTS `t_user`; </update> <!-- 建表 --> <update id="createTable"> CREATE TABLE IF NOT EXISTS `t_user` ( `id` bigint(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) DEFAULT NULL, `age` int(11) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; </update> <!-- 保存 --> <insert id="insert" parameterType="User" useGeneratedKeys="true" keyProperty="id"> insert into t_user(name,age) values(#{name},#{age}) </insert> <!-- 查询 --> <select id="findByKey" resultType="User" parameterType="long"> select id,name,age from t_user where id=#{id} </select> <!-- 获取所有 --> <select id="findAll" resultType="User"> select id,name,age from t_user </select> <!-- 修改 --> <update id="update" parameterType="User"> update t_user set name=#{name},age=#{age} where id=#{id} </update> <!-- 删除 --> <delete id="delete" parameterType="long"> delete from t_user where id=#{id} </delete></mapper>

测试类SingleTableTest

package com.jege.mybatis;import java.io.Reader;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.After;import org.junit.Before;import org.junit.BeforeClass;import org.junit.Test;import com.jege.mybatis.mapper.User;/** * @author JE哥 * @email 1272434821@qq.com * @description:单表CRUD Test */public class SingleTableTest { private static final String NAME_SPACE = "com.jege.mybatis.mapper.UserMapper"; private static SqlSessionFactory sqlSessionFactory = null; private SqlSession sqlSession = null; @BeforeClass public static void setUpBeforeClass() throws Exception { Reader reader = Resources.getResourceAsReader("mybatis-config.xml"); sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); } @Before public void setUp() throws Exception { sqlSession = sqlSessionFactory.openSession(); sqlSession.insert(NAME_SPACE + ".dropTable"); sqlSession.insert(NAME_SPACE + ".createTable"); sqlSession.commit(); } // mybatis会通过mapper文件配置把保存后的主键放到当前对象的id里面 @Test public void insert() throws Exception { sqlSession = sqlSessionFactory.openSession(); User user = new User("je-ge", 22); System.out.println("保存之前:" + user); sqlSession.insert(NAME_SPACE + ".insert", user); sqlSession.commit(); System.out.println("保存之后:" + user); System.out.println("+++++++++++++++"); } @Test public void update() throws Exception { insert(); User user = sqlSession.selectOne(NAME_SPACE + ".findByKey", 1L); System.out.println("修改前:" + user); user.setName("JE-GE"); user.setAge(18); sqlSession.update(NAME_SPACE + ".update", user); sqlSession.commit(); user = sqlSession.selectOne(NAME_SPACE + ".findByKey", 1L); System.out.println("修改后:" + user); } @Test public void findByKey() throws Exception { User user = sqlSession.selectOne(NAME_SPACE + ".findByKey", 1L); System.out.println(user); } @Test public void findAll() throws Exception { List<User> users = sqlSession.selectList(NAME_SPACE + ".findAll"); for (User user : users) { System.out.println(user); } } @Test public void delete() throws Exception { User user = sqlSession.selectOne(NAME_SPACE + ".findByKey", 1L); System.out.println("删除前:" + user); sqlSession.delete(NAME_SPACE + ".delete", 1L); sqlSession.commit(); user = sqlSession.selectOne(NAME_SPACE + ".findByKey", 1L); System.out.println("删除后:" + user); } @After public void tearDown() throws Exception { if (sqlSession != null) sqlSession.close(); }}

其他关联项目

MyBatis 菜鸟教程1-环境配置 http://blog.csdn.net/je_ge/article/details/53998874

源码地址

https://github.com/je-ge/mybatis

如果觉得我的文章或者代码对您有帮助,可以请我喝杯咖啡。 您的支持将鼓励我继续创作!谢谢! 微信打赏 支付宝打赏


上一篇:Java NIO浅析

下一篇:类集框架(一)

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表