首页 > 代码库 > mybatis(单表增删改查useMapper版)
mybatis(单表增删改查useMapper版)
数据库脚本(注意测试时先add-->load-->update-->delete-->list)
UserMapper版
-- -------------------------------------------------------- -- 主机: 127.0.0.1 -- 服务器版本: 5.5.36-MariaDB - mariadb.org binary distribution -- 服务器操作系统: Win32 -- HeidiSQL 版本: 8.0.0.4396 -- -------------------------------------------------------- /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET NAMES utf8 */; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE=‘NO_AUTO_VALUE_ON_ZERO‘ */; -- 导出 mybatis 的数据库结构 CREATE DATABASE IF NOT EXISTS `mybatis` /*!40100 DEFAULT CHARACTER SET utf8 */; USE `mybatis`; -- 导出 表 mybatis.t_user 结构 CREATE TABLE IF NOT EXISTS `t_user` ( `id` int(10) NOT NULL AUTO_INCREMENT, `username` varchar(50) DEFAULT NULL, `password` varchar(50) DEFAULT NULL, `nickname` varchar(50) DEFAULT NULL, `type` tinyint(4) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
mybatis-config.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" /> <typeAliases> <!--把edu.hhxy.btaisUser类都自动映射为User --> <typeAlias type="edu.hhxy.btais.User" alias="User" /> <!-- 把edu.hhxy.btais所有类都自动映射 --> <package name="edu.hhxy.btais" /> </typeAliases> <!-- 在properties之后加上typeAliases --> <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <!-- 配置mybatis的pooled的数据源 --> <dataSource type="POOLED"> <property name="driver" value="http://www.mamicode.com/${driver}" /> <property name="url" value="http://www.mamicode.com/${url}" /> <property name="username" value="http://www.mamicode.com/${username}" /> <property name="password" value="http://www.mamicode.com/${password}" /> </dataSource> </environment> </environments> <mappers> <mapper resource="edu/hhxy/btais/UserMapper.xml" /> </mappers> </configuration>
UserMapper.java
package edu.hhxy.btais.mapper; import java.util.List; import edu.hhxy.btais.User; public interface UserMapper { /* * 基于annotation的方法知道就行了 */ // @Insert("insert into t_user(username,password,nickname,type) value(#{username},#{password},#{nickname},#{type})") public void add(User user); public void insert(User user); public void update(User user); public void delete(User user); public User load(Integer integer); public List<User> list(); }
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="edu.hhxy.btais.mapper.UserMapper"> <insert id="add" parameterType="User"> insert into t_user (username,password,nickname,type) value(#{username},#{password},#{nickname},#{type}) </insert> <update id="update" parameterType="User"> update t_user set password=#{password},nickname=#{nickname},type=#{type} where id=#{id} </update> <delete id="delete" parameterType="int"> delete from t_user where id=#{id} </delete> <select id="load" parameterType="int" resultType="User"> select * from t_user where id=#{id} </select> <select id="list" resultType="User"> select * from t_user </select> </mapper>
TestMybatisByMapper.java
package edu.hhxy.btais; import java.util.List; import org.apache.ibatis.session.SqlSession; import org.junit.Test; import edu.hhxy.btais.mapper.UserMapper; import edu.hhxy.btais.util.MyBatisUtil; public class TestMybatisByMapper { @Test public void test() { addTest();
loadTest(); updateTest();
listTest(); deleteTest(); } @Test public void addTest() { SqlSession session = null; try { session = MyBatisUtil.createSeqSession(); User u = new User(); u.setNickname("猪八戒"); u.setPassword("123"); u.setType(1); u.setUsername("bajie"); session.getMapper(UserMapper.class).add(u); session.commit(); } catch (Exception e) { e.printStackTrace(); } finally { MyBatisUtil.closeSeqSession(session); } } @Test public void updateTest() { SqlSession session = null; try { session = MyBatisUtil.createSeqSession(); User u = new User(); u.setNickname("齐天大圣孙悟空"); u.setPassword("123"); u.setType(0); u.setId(1); u.setUsername("wukong"); session.getMapper(UserMapper.class).update(u); session.commit(); } catch (Exception e) { e.printStackTrace(); } finally { MyBatisUtil.closeSeqSession(session); } } @Test public void deleteTest() { SqlSession session = null; try { session = MyBatisUtil.createSeqSession(); User u = new User(); u.setId(1); session.getMapper(UserMapper.class).delete(u); session.commit(); } catch (Exception e) { e.printStackTrace(); } finally { MyBatisUtil.closeSeqSession(session); } } @Test public void loadTest() { SqlSession session = null; try { session = MyBatisUtil.createSeqSession(); User u = (User) session.getMapper(UserMapper.class).load(3); System.out.println(u.toString()); } catch (Exception e) { e.printStackTrace(); } finally { MyBatisUtil.closeSeqSession(session); } } @Test public void listTest() { SqlSession session = null; try { session = MyBatisUtil.createSeqSession(); List<User> us =session.getMapper(UserMapper.class).list(); System.out.println(us.size()); } catch (Exception e) { e.printStackTrace(); } finally { MyBatisUtil.closeSeqSession(session); } } }
运行前:
junit:
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。