MyBatis的增删改查简单操作
1.准备工作
1.1 创建Maven项目,导入POM坐标
<?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">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>mybatis01</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<!--mysql驱动坐标-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</version>
</dependency>
</dependencies>
</project>
1.2 准备核心配置文件
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.user=root
jdbc.password=root
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<!--mybatis核心配置文件-->
<configuration>
<!--加载外置的properties文件-->
<properties resource="jdbc.properties"></properties>
<!--给类设置别名-->
<typeAliases>
<typeAlias type="com.other.domain.User" alias="user"/>
</typeAliases>
<!--配置数据库连接环境-->
<environments default="development">
<!--development环境-->
<environment id="development">
<!--使用JDBC的事务管理器-->
<transactionManager type="JDBC"/>
<!--使用数据库连接池-->
<dataSource type="POOLED">
<!--连接属性-->
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<!--配置Mapper-->
<mappers>
<mapper resource="mapper/UserMapper.xml"/>
</mappers>
</configuration>
1.3 创建表sys_user,实体类User
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for sys_user
-- ----------------------------
DROP TABLE IF EXISTS `sys_user`;
CREATE TABLE `sys_user` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`username` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`email` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`password` varchar(80) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`phoneNum` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of sys_user
-- ----------------------------
INSERT INTO `sys_user` VALUES (1, 'zhangsan', 'zhangsan@itcast.cn', '123', '13888888888');
INSERT INTO `sys_user` VALUES (2, 'lisi', 'lisi@itcast.cn', '123', '13999999999');
INSERT INTO `sys_user` VALUES (3, 'wangwu', 'wangwu@itcast.cn', '123', '18599999999');
SET FOREIGN_KEY_CHECKS = 1;
package com.other.domain;
public class User {
private int id;
private String username;
private String email;
private String password;
private String phoneNum;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhoneNum() {
return phoneNum;
}
public void setPhoneNum(String phoneNum) {
this.phoneNum = phoneNum;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", email='" + email + '\'' +
", password='" + password + '\'' +
", phoneNum='" + phoneNum + '\'' +
'}';
}
}
1.4 编写获取SqlSession的静态方法
static SqlSession getInstance() throws IOException {
InputStream config = Resources.getResourceAsStream("SqlMapperConfig.xml");
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
SqlSessionFactory sqlSessionFactory = builder.build(config);
SqlSession sqlSession = sqlSessionFactory.openSession();
return sqlSession;
}
2.添加操作
<?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="UserMapper">
<insert id="save" parameterType="user">
insert into sys_user values(#{id},#{username},#{email},#{password},#{phoneNum})
</insert>
</mapper>
@Test
public void testAdd() throws IOException {
SqlSession sqlSession = getInstance();
User user = new User();
user.setUsername("tom");
user.setEmail("123455@qq.com");
user.setPassword("qwueioqiwjdojasjdzx");
user.setPhoneNum("1231231231231");
int insert = sqlSession.insert("UserMapper.save", user);
System.out.println(insert);
sqlSession.commit();
sqlSession.close();
}
3.更新操作
<?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="UserMapper">
<update id="update" parameterType="user">
update sys_user
set
username=#{username},
password=#{password},
email=#{email},
phoneNum=#{phoneNum}
where id=#{id};
</update>
</mapper>
@Test
public void testUpdate() throws IOException {
SqlSession sqlSession = getInstance();
User user = new User();
user.setId(4);
user.setUsername("lucy");
user.setEmail("123455@qq.com");
user.setPassword("asdasdasd");
user.setPhoneNum("1111111111111");
int update = sqlSession.update("UserMapper.update", user);
System.out.println(update);
sqlSession.commit();
sqlSession.close();
}
4.删除操作
<?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="UserMapper">
<delete id="delete" parameterType="int">
delete from sys_user where id=#{id}
</delete>
</mapper>
@Test
public void testDel() throws IOException {
SqlSession sqlSession = getInstance();
int delete = sqlSession.delete("UserMapper.delete", 5);
System.out.println(delete);
sqlSession.commit();
sqlSession.close();
}
|