相关的结构
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="db.properties"></properties>
<!-- 日志的相关的信息的配置-->
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<!--别名配置-->
<!-- 配置方式一 -->
<!-- <typeAliases>
<typeAlias type="com.zjj.pojo.User" alias="User"/>
</typeAliases>-->
<!-- <!– 配置方式二 –>-->
<typeAliases>
<package name="com.zjj.pojo"/>
</typeAliases>
<!-- 全局配置参数,需要时再设置 -->
<!-- http://www.mybatis.org/mybatis-3/zh/configuration.html -->
<!--配置连接数据库配置信息-->
<environments default="development">
<environment id="development">
<transactionManager type="jdbc"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<!--引入映射文件-->
<mappers>
<mapper resource="mapper/UserMapper.xml"/>
<mapper resource="mapper/StudentMapper.xml"/>
</mappers>
</configuration>
数据库的相关的表的建立:
/*
Navicat Premium Data Transfer
Source Server : ssm
Source Server Type : MySQL
Source Server Version : 50733
Source Host : localhost:3306
Source Schema : ssm
Target Server Type : MySQL
Target Server Version : 50733
File Encoding : 65001
Date: 23/04/2021 08:52:18
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for address
-- ----------------------------
DROP TABLE IF EXISTS `address`;
CREATE TABLE `address` (
`aid` int(11) NOT NULL AUTO_INCREMENT,
`address` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`aid`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of address
-- ----------------------------
INSERT INTO `address` VALUES (1, '邵阳');
INSERT INTO `address` VALUES (2, '广州');
INSERT INTO `address` VALUES (3, '东莞');
-- ----------------------------
-- Table structure for educationexperience
-- ----------------------------
DROP TABLE IF EXISTS `educationexperience`;
CREATE TABLE `educationexperience` (
`eid` int(11) NOT NULL AUTO_INCREMENT,
`college` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`highschool` varchar(10) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`middleschool` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`primaryschool` varchar(10) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`time` date NULL DEFAULT NULL,
PRIMARY KEY (`eid`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of educationexperience
-- ----------------------------
INSERT INTO `educationexperience` VALUES (1, 'jishoudaxue', 'yizhong', 'huayuan', 'huayuan', '2018-05-09');
INSERT INTO `educationexperience` VALUES (2, '清华大学', '洞口一中', '花园中学', '花园小学', '2021-04-06');
-- ----------------------------
-- Table structure for student
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`sid` int(11) NOT NULL,
`email` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`name` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`telephone` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`age` int(11) NULL DEFAULT NULL,
`aid` int(11) NULL DEFAULT NULL,
`eid` int(11) NULL DEFAULT NULL,
PRIMARY KEY (`sid`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `student` VALUES (1, '2282240015@qq.com', '卢红兰', '17886977662', 20, 1, 1);
INSERT INTO `student` VALUES (2, '333434@', ' 丁雪键', '1788698222', 22, 2, 2);
-- ----------------------------
-- Table structure for tb_clazz
-- ----------------------------
DROP TABLE IF EXISTS `tb_clazz`;
CREATE TABLE `tb_clazz` (
`cid` int(11) NULL DEFAULT NULL,
`cname` varchar(20) CHARACTER SET latin1 COLLATE latin1_swedish_ci NULL DEFAULT NULL,
`classroom` varchar(20) CHARACTER SET latin1 COLLATE latin1_swedish_ci NULL DEFAULT NULL
) ENGINE = InnoDB CHARACTER SET = latin1 COLLATE = latin1_swedish_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of tb_clazz
-- ----------------------------
INSERT INTO `tb_clazz` VALUES (1, 'ruanjianyiban', '2123');
-- ----------------------------
-- Table structure for tb_stu
-- ----------------------------
DROP TABLE IF EXISTS `tb_stu`;
CREATE TABLE `tb_stu` (
`sid` char(20) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL,
`sname` varchar(20) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL,
`birthday` date NULL DEFAULT NULL,
`address` varchar(9) CHARACTER SET latin1 COLLATE latin1_swedish_ci NULL DEFAULT NULL,
`cid` int(20) NOT NULL,
`tid` int(6) NULL DEFAULT NULL,
PRIMARY KEY (`sid`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = latin1 COLLATE = latin1_swedish_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of tb_stu
-- ----------------------------
INSERT INTO `tb_stu` VALUES ('1', 'houzhicong', '2021-04-22', 'hunandon', 1, 1);
-- ----------------------------
-- Table structure for tb_teacher
-- ----------------------------
DROP TABLE IF EXISTS `tb_teacher`;
CREATE TABLE `tb_teacher` (
`tid` int(11) NULL DEFAULT NULL,
`tname` varchar(20) CHARACTER SET latin1 COLLATE latin1_swedish_ci NULL DEFAULT NULL,
`tsubject` varchar(10) CHARACTER SET latin1 COLLATE latin1_swedish_ci NULL DEFAULT NULL
) ENGINE = InnoDB CHARACTER SET = latin1 COLLATE = latin1_swedish_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of tb_teacher
-- ----------------------------
INSERT INTO `tb_teacher` VALUES (1, 'houzhicong', 'computer');
-- ----------------------------
-- Table structure for tb_user
-- ----------------------------
DROP TABLE IF EXISTS `tb_user`;
CREATE TABLE `tb_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(30) CHARACTER SET latin1 COLLATE latin1_swedish_ci NULL DEFAULT NULL,
`password` varchar(16) CHARACTER SET latin1 COLLATE latin1_swedish_ci NULL DEFAULT NULL,
`isadmin` int(11) NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 21 CHARACTER SET = latin1 COLLATE = latin1_swedish_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of tb_user
-- ----------------------------
INSERT INTO `tb_user` VALUES (1, 'dingxuejian', 'updatesucess', 1);
INSERT INTO `tb_user` VALUES (2, 'dingxuejian', '12', 2);
INSERT INTO `tb_user` VALUES (3, 'dingxuejian', '23', 1);
INSERT INTO `tb_user` VALUES (4, 'dingxuejian', '66', 1);
INSERT INTO `tb_user` VALUES (5, 'dingxuejian', '123456', 1);
INSERT INTO `tb_user` VALUES (6, 'dingxuejian', '123456', 1);
INSERT INTO `tb_user` VALUES (7, 'dingxuejian', '234', 1);
INSERT INTO `tb_user` VALUES (8, 'dingxuejian', 'enen', 1);
INSERT INTO `tb_user` VALUES (9, 'dingxuejian', 'hou', 1);
INSERT INTO `tb_user` VALUES (10, 'dingxuejian', 'zhi', 2);
INSERT INTO `tb_user` VALUES (11, 'dingxuejian', 'cong', 1);
INSERT INTO `tb_user` VALUES (12, 'dingxuejian', 'enen', 1);
INSERT INTO `tb_user` VALUES (13, 'dingxuejian', 'hou', 1);
INSERT INTO `tb_user` VALUES (14, 'dingxuejian', 'zhi', 2);
INSERT INTO `tb_user` VALUES (15, 'dingxuejian', 'cong', 1);
INSERT INTO `tb_user` VALUES (16, 'dingxuejian', 'enen', 1);
INSERT INTO `tb_user` VALUES (17, 'dingxuejian', 'hou', 1);
INSERT INTO `tb_user` VALUES (18, 'dingxuejian', 'zhi', 2);
INSERT INTO `tb_user` VALUES (19, 'dingxuejian', 'cong', 1);
INSERT INTO `tb_user` VALUES (20, 'dingxuejian', NULL, NULL);
SET FOREIGN_KEY_CHECKS = 1;
pom.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>TeacherSpringCompnentScan</artifactId>
<groupId>com.zjj</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>TeacherMybatis</artifactId>
<dependencies>
<!-- mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.45</version>
</dependency>
<!-- mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<!--junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<!-- log4j-->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.8.2</version>
</dependency>
</dependencies>
</project>
配置相关的日志:
在mybatis-config.xml文件中加上:
<!-- 日志的相关的信息的配置-->
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
在ReSource目录中创建log4j.properties文件加上如下的内容:
log4j.rootLogger=debug,stdout,logfile
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.SimpleLayout
log4j.logger.com.ibatis=DEBUG
log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=DEBUG
log4j.logger.com.ibatis.common.jdbc.ScriptRunner=DEBUG
log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=DEBUG
log4j.logger.Java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
StudentMapper.xml文件:
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zjj.dao.StudentDao">
<!--
<!– 一对一对应数据库的字段名–>
<resultMap id="stuResultMap" type="Student">
<id property="stuId" column="sid"/>
<result property="stuName" column="sname"/>
<result property="birthday" column="birthday"/>
<result property="address" column="address"/>
<!– 一个学生对应一个班级 一对一的association注释来玩 –>
<association property="clazz" javaType="Clazz" resultMap="clazzResultMap"/>
</resultMap>
<resultMap id="clazzResultMap" type="Clazz">
<id property="id" column="cid"/>
<result property="className" column="cname"/>
<result property="classRoom" column="classroom"/>
</resultMap>
<resultMap id="teacherResultMap" type="Teacher">
<id property="teacherId" column="tid"/>
<result property="teacherName" column="tname"/>
<result property="subject" column="tsubject"/>
<!–一对多 collection 集合 ofType集合里面的泛型–>
<!– students是Teacher表中的一个属性名 List<Student> students–>
<collection property="students" ofType="Student" resultMap="stuResultMap"/>
</resultMap>
<select id="queryAllStudents" resultMap="stuResultMap">
SELECT * from tb_stu s,tb_clazz c where s.cid=c.cid
</select>
<select id="queryTeacherById" parameterType="int" resultMap="teacherResultMap">
select * from tb_teacher t,tb_stu s where t.tid=s.tid and t.tid=#{id}
</select>-->
<resultMap id="studentResultMap" type="Student">
<id property="sid" column="sid"></id>
<result property="email" column="email"></result>
<result property="name" column="name"></result>
<result property="telephone" column="telephone"></result>
<result property="age" column="age"></result>
<association property="address" javaType="Address" resultMap="addressResultMap"></association>
</resultMap>
<resultMap id="addressResultMap" type="Address">
<id property="aid" column="aid"></id>
<result property="address" column="address"></result>
</resultMap>
<!-- private Integer eid;-->
<!-- private String college;-->
<!-- private String highschool;-->
<!-- private String primaryschool;-->
<!-- private Date time;-->
<!-- List<Student> students;//一对多-->
<resultMap id="educationResultMap" type="Education">
<id property="eid" column="eid"></id>
<result property="college" column="college"></result>
<result property="highschool" column="highschool"></result>
<result property="primaryschool" column="primaryschool"></result>
<result property="time" column="time"></result>
<collection property="students" ofType="Student" resultMap="studentResultMap"></collection>
</resultMap>
<!--单表查询-->
<select id="queryAll" resultMap="studentResultMap">
select *from student
</select>
<!-- 连表查询 通过id查询-->
<select id="queryAllStudentsBysid" parameterType="int" resultMap="studentResultMap">
select *from student s,address a where s.aid=a.aid
</select>
<!-- 一对多表的查询 -->
<select id="queryStudentsEducationByid" parameterType="int" resultMap="educationResultMap">
select *from student s, educationexperience ee where s.eid=ee.eid and
s.eid=#{eid}
</select>
<!-- if test查询-->
<select id="queryStudentByEmailAndName" parameterType="Student" resultMap="studentResultMap">
select *from student where 1=1
<if test="email!=null and email!=''">
and email=#{email}
</if>
<if test="name!=null and name!=''">
and name=#{name}
</if>
</select>
<!-- where -->
<select id="queryStudentByEmailAndName2" parameterType="Student" resultMap="studentResultMap">
select *from student
<where>
<if test="email!=null and email!=''">
and email=#{email}
</if>
<if test="name!=null and name!=''">
and name=#{name}
</if>
</where>
</select>
<!-- set标签-->
<update id="updateStudentBySet" parameterType="Student">
update student
<set>
<if test="email!=null and email!=''">
email=#{email}
</if>
<if test="name!=null and name!=''">
name=#{name}
</if>
<if test="telephone!=null and telephone!=''">
telephone=#{telephone}
</if>
<if test="age!=0 and age!=null">
age=#{age}
</if>
</set>
</update>
</mapper>
UserMapper.xml文件:
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace 值必须和接口路径保持一致-->
<mapper namespace="com.zjj.dao.UserDao">
<!-- ResultMap是属性名和字段名冲突-->
<resultMap id="userResultMap" type="User">
<!-- id表示主键 result普通属性 association javabean collection 集合-->
<id property="id" column="id"/>
<result property="username" column="username"/>
<result property="password" column="password"/>
<result property="isadmin" column="isadmin"/>
</resultMap>
<!-- id值必须和接口里面的方法名保持一致 -->
<select id="queryById" resultMap="userResultMap">
select * from tb_user where id=#{id}
</select>
<!--查询所有 -->
<select id="queryAllUsers" resultType="User">
select *from tb_user
</select>
<!-- 模糊查询-->
<select id="queryUsersBymouhu" parameterType="string" resultMap="userResultMap">
select *from tb_user where username like "%${mohu}%"
</select>
<!-- 多个模糊查询-->
<select id="queryUsersBymouhu2" parameterType="string" resultMap="userResultMap">
select *from tb_user where username like "%${mo}%" or password like "%${mo}%"
</select>
<!-- 直接查询-->
<!-- 添加 id为自增主键 keyProperty="id" useGeneratteKeys="true"-->
<insert id="addUser" parameterType="User" >
INSERT INTO tb_user ( username, password, isadmin) VALUES ( #{username},#{password} ,#{isadmin} )
</insert>
<insert id="addUser2" parameterType="User" keyProperty="id" useGeneratedKeys="true">
insert into tb_user(username,password,isadmin) values(#{username},#{password},#{isadmin})
</insert>
<!--修改更新-->
<update id="updateUser" parameterType="User">
UPDATE `tb_user` SET `username` = #{username}, `password` = #{password}, `isadmin` = #{isadmin} WHERE `id` = #{id};
</update>
<!--删除 -->
<delete id="deleteById" parameterType="long">
delete from tb_user where id=#{id}
</delete>
<!-- queryStudentsByUsernameAndAdmin-->
<select id="queryStudentsByUsernameAndAdmin" parameterType="User" resultMap="userResultMap">
select *from tb_user where username=#{username} and isadmin=#{isadmin}
</select>
<!--根据多个id查询queryByUserIds-->
<select id="queryUserByids" parameterType="List" resultMap="userResultMap">
select *from tb_user where id in
-- 注意这里的list一定要小写
<foreach collection="list" item="id" index="i" open="(" separator="," close=")">
#{id}
</foreach>
</select>
<!-- 批量添加Users-->
<insert id="insertBatch" parameterType="List">
INSERT INTO tb_user ( username, password, isadmin) VALUES
<foreach collection="list" item="item" index="index" separator=",">
(#{item.username}, #{item.password}, #{item.isadmin})
</foreach>
</insert>
</mapper>
User文件:
package com.zjj.pojo;
public class User {
// 建议属性名和字段名保持一致,这个可以不用resultMap
private Long id;
private String username;
private String password;
private Integer isadmin;
public User() {
}
public User(String username, String password, Integer isadmin) {
this.username = username;
this.password = password;
this.isadmin = isadmin;
}
public User(Long id, String username, String password, Integer isadmin) {
this.id = id;
this.username = username;
this.password = password;
this.isadmin = isadmin;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getIsadmin() {
return isadmin;
}
public void setIsadmin(Integer isadmin) {
this.isadmin = isadmin;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", isadmin=" + isadmin +
'}';
}
}
UserDao文件:
package com.zjj.dao;
import com.zjj.pojo.User;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface UserDao{
//测试一下看看是否有连接的错误
public User queryById(Long id);
// 查询所有的User
public List<User> queryAllUsers();
// 通过id删除数据
public void deleteById(Long id);
// 添加一个User
public void addUser(User user);
// 添加并返回添加后的自增主键
public void addUser2(User user);
// 更新
public void updateUser(User user);
// 模糊查询
public List<User> queryUsersBymouhu(@Param("mohu") String mohu);
// 多个模糊查询
public List<User> queryUsersBymouhu2(@Param("mo") String mo);
// 查询直接查询
public List<User> queryUserByUsernameAndisadmin(User user);
// 根据id的集合查询所有
public List<User> queryUserByids(List<Long> ids);
// 批量添加
public void insertBatch(List<User> users);
}
创建userTest类
单元测试的相关的代码:
package test;
import com.zjj.dao.StudentDao;
import com.zjj.dao.UserDao;
import com.zjj.pojo.User;
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.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
public class userTest {
SqlSession session;
UserDao userDao;
StudentDao studentDao;
@Before
public void getSession() throws IOException {
//加载配置文件 并读取 通过IO流
//1. 获取输入流
InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
session = sqlSessionFactory.openSession(true);
userDao=session.getMapper(UserDao.class);
studentDao=session.getMapper(StudentDao.class);
}
@After
public void close(){
session.close();
}
@Test
//查询一个User
public void queryByIdtest(){
User user = userDao.queryById(1L);
System.out.println(user);
}
// 查询所有的User
@Test
public void queryAllUsertest(){
List<User> users = userDao.queryAllUsers();
for (User user:users) {
System.out.println(user);
}
}
@Test
public void updateUsertest(){
User user=new User(1L,"update","updatesucess",1);
userDao.updateUser(user);
}
//测试删除
@Test
public void testDelete() throws IOException {
userDao.deleteById(1L);
}
//测试模糊查询
@Test
public void queryUsersBymouhutest() throws IOException {
List<User> fan = userDao.queryUsersBymouhu("fan");
System.out.println(fan);
}
//测试多个模糊查询
@Test
public void queryUsersBymouhu2test() throws IOException {
List<User> users = userDao.queryUsersBymouhu2("2");
System.out.println(users);
}
// 增加方法的测试
@Test
public void addOneUser() throws IOException {
UserDao userDao=session.getMapper(UserDao.class);
User user=new User("mama","123456",1);
userDao.addUser(user);
session.close();
}
// addUser2方法测试
@Test
public void addUser2test(){
User user=new User("tengmingghan","234",1);
userDao.addUser2(user);
System.out.println(user);
//打印出的user的信息 User{id=7, username='tengmingghan', password='234', isadmin=1}
}
@Test
public void queryStudentsByUsernameAndAdmintest(){
User user=new User();
// user.setUsername("houfang");
user.setIsadmin(2);
// 打印查询出的User
List<User> users=userDao.queryUserByUsernameAndisadmin(user);
System.out.println(users);
}
@Test
public void queryUserByidstest(){
List<Long> ids=new ArrayList<Long>();
ids.add(1L);
ids.add(3L);
ids.add(2L);
ids.add(4L);
List<User> users = userDao.queryUserByids(ids);
System.out.println("通过id批量的查询"+users);
}
// insertBatch测试
@Test
public void insertBatchtest(){
User user1=new User("number7","enen",1);
User user2=new User("number8","hou",1);
User user3=new User("number9","zhi",2);
User user4=new User("number10","cong",1);
List<User> users=new ArrayList<User>();
users.add(user1);
users.add(user2);
users.add(user3);
users.add(user4);
userDao.insertBatch(users);
}
// if test条件的测试
@Test
public void queryByUsersUsernameandIsadmin2test(){
User user=new User();
// user.setIsadmin(2);
user.setUsername("lijun");
List<User> users = userDao.queryUserByUsernameAndisadmin2(user);
System.out.println(users);
}
//测试where标签
@Test
public void queryByUsersUsernameandIsadmin3test(){
User user=new User();
// user.setIsadmin(2);
user.setUsername("lijun");
List<User> users = userDao.queryUserByUsernameAndisadmin2(user);
System.out.println(users);
}
// 测试trim标签
@Test
public void queryByUsersUsernameandIsadmin4test(){
User user=new User();
// user.setIsadmin(2);
user.setUsername("lijun");
List<User> users = userDao.queryUserByUsernameAndisadmin2(user);
System.out.println(users);
}
// 通过Set标签进行更新
@Test
public void updateUserBySettest(){
User user=new User();
user.setUsername("dingxuejian");
user.setId(1L);
userDao.updateUserBySet(user);
}
}