app开发者平台在数字化时代的重要性与发展趋势解析
765
2023-03-18
基于Mybatis实现CRUD操作过程解析(xml方式)
1、环境搭建
1.1 表结构
create table user (
id int primary key auto_increment,
username varchar(20) not null,
birthday date,
sex char(1) default '男',
address varchar(50)
);
insert into user values (null, '孙悟空','1980-10-24','男','花果山水帘洞');
insert into user values (null, '白骨精','1992-11-12','女','白虎岭白骨洞');
insert into user values (null, '猪八戒','1983-05-20','男','福临山云栈洞');
insert into user values (null, '蜘蛛精','1995-03-22','女','盤丝洞');
1.2 创建项目
导入如下jar
mybatis框架包
数据库驱动包
log4j日志包
junit单元测试包
1.3 准备配置文件
在src下准备配置文件:sqlMapConfig.xml
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybaQMbJMwtis-3-config.dtd">
QMbJMw
在 src 下准备配置文件:log4j.properties
# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
2-增删改查源码
1.1 执行结果
1.2 User类代码
import java.sql.Date;
/**
用户实体类对象 */
public class User {
private Integer id;
private String username;
private Date birthday;
private String sex;
private String address;
public User() {
}
public User(Integer id, String username, Date birthday, String sex, String address) {
this.id = id;
this.username = username;
this.birthday = birthday;
this.sex = sex;
this.address = address;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", birthday=" + birthday +
", sex='" + sex + '\'' +
", address='" + address + '\'' +
'}';
}
}
1.3 UserMapper接口代码
package com.vg.dao;
import com.vg.entity.User;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* 数据访问层接口:对用户进行增删改查操作
*/
public interface UserMapper {
/**
* 添加用户
*/
int addUser(User user);
/**
* 根据id删除用户
*/
void deleteUser(Integer id);
/**
* 通过id修改用户信息
*/
/**
* 修改用户信息
*/
void updateUser(User user);
/**
* 查询所有用户
*/
List
/**
* 根据id查询用户
*/
User findUserById(int id);
/**
* 根据用户名模糊查询用户
*/
List
/**
* 根据姓名和性别查询用户数据(姓名使用模糊查询)
*/
/**
* 根据用户名称模糊查询用户
*/
}
}
1.4 UserMapper.xml接口映射文件
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
select * from user
select * from user where id = #{id};
select *from user where username like #{username}
insert into user values(null,#{username},#{birthday},#{sex},#{address});
update user set username = #{username},
birthday = #{birthday},sex = #{sex}, address = #{address}
where id = #{id}
delete from user where id = #{id}
1.5 在主配置文件中加载接口映射文件
1.6 测试类代码
package com.vg.test;
import com.vg.dao.UserMapper;
import com.vg.entity.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.BeforeClass;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Date;
import java.util.List;
/**
* 测试类
*/
public class TestUserMapper {
private static SqlSessionFactory sqlSessionFactory =null;
private SqlSession sqlSession =null;
private UserMapper userMapper =null;
// 该方法在所有测试方法执行之前执行1次
@BeforeClass
public static void init() throws Exception {
// 1. 通过Resources类,加载sqlMapConfig.xml,得到文件输入流对
InputStream in = Resources.getResourceAsStream("sqlMapConfig.xml");
// 2. 实例化会话工厂创建类SqlSessionFactoryBuilder对象
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
// 3. 根据字节输入流获取SqlSessionFactory对象
sqlSessionFactory = builder.build(in);
}
// 该方法会在每个测试方法执行之前执行1次
@Before
public void before(){
// 4. 获取SqlSession对象,等价连接对象
// true:事务自动提交,false:不自动提交,默认值
sqlSession = sqlSessionFactory.openSession();
// 5. 创建接口 实现类对象
userMapper = sqlSession.getMapper(UserMapper.class);
}
/**
* 测试方法:通过用户名模糊查询用户
*/
@Test
public void testAddUser() throws Exception{
// 1. 获得接口代理对象
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
// 2. 创建User对象
User u = new User();
u.setUsername("如来佛祖111");
u.setBirthday(Date.valueOf("1980-01-20"));
u.setAddress("西天灵山");
u.setSex("男");
// 3. 保存用户信息
int row = userMapper.addUser(u);
System.out.println("row = " + row);
// 4. 手动提交事务
sqlSession.commit();
}
/**
* 测试方法:通过用户名模糊查询用户
*/
@Test
public void testFindUserByName() throws Exception{
List
for (User user : userList) {
System.out.println(user);
}
}
/**
* 测试方法:查询所有用户
*/
@Test
public void testFindAllUsers() throws Exception{
List
for (User user : userList) {
System.out.println(user);
}
}
/**
* 根据id查询用户
* @throws Exception
*/
@Test
public void testFindUserById()throws Exception{
// 6. 调用接口的方法根据id查询用户
User user = userMapper.findUserById(1);
System.out.println("user = " + user);
}
@Test
public void testUpdateUser()throws Exception{
// 创建用户对象
User user = new User(
6,
"牛魔王",
Date.valueOf("1998-02-20"),
"妖",
"牛魔洞");
// 更新用户
userMapper.updateUser(user);
// 提交事务
sqlSession.commit();
}
@Test
public void testDeleteUser()throws Exception{
// 1. 获得接口代理对象
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
// 2. 根据id删除用户
userMapper.deleteUser(6);
// 3. 提交事务
sqlSession.commit();
}
@After
public void after() {
// 7. 关闭会话,释放资源。
sqlSession.close();
}
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~