使用 Maven 搭建 Mybatis 环境:https://blog.csdn.net/qq_42322548/article/details/89913220
创建一个 Maven 项目。
1、添加 jar 包依赖。
org.mybatis
mybatis
3.5.1
mysql
mysql-connector-java
8.0.13
junit
junit
4.12
org.projectlombok
lombok
1.18.8
provided
2、设置资源文件路径,使 Maven 能编译 resource 文件夹之外的资源文件。
src/main/java
**/*.properties
**/*.xml
false
3、目录结构。
这里使用了 Lombok:https://blog.csdn.net/qq_42322548/article/details/91438451
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private int id;
private String account;
private String password;
private String name;
private int phone;
}
在 resource 文件夹新建 jdbc.properties 配置文件,保存配置相关的信息。
# mysql驱动包名
driver=com.mysql.cj.jdbc.Driver
# 数据库连接地址
url=jdbc:mysql://localhost:3306/demo_1? serverTimezone=GMT &useUnicode=true& characterEncoding=utf8
# 用户名
username=root
# 密码
password=root
在 resource 文件夹新建 mybatis-config.xml 。
在 mapper 文件夹新建 UserMapper.xml 文件,并在 Configuration.xml 文件中加载该文件。
insert into users values (#{id}, #{account}, #{password}, #{name}, #{phone})
delete from users where id = #{id}
update users set account = #{account}, password = #{password}, name = #{name}, phone = #{phone} where id = #{id}
在 dao 文件夹中新建 UserDao 接口。
public interface UserDao {
/**
* 增
* @param user
*/
void insertUser(User user);
/**
* 删
* @param id
*/
void deleteUser(int id);
/**
* 改
* @param user
*/
void updateUser(User user);
/**
* 通过id查找用户
* @param id
* @return
*/
User queryUserById(int id);
/**
* 通过姓名模糊查找用户
* @param name
* @return
*/
List queryUserByName(String name);
}
在 dao 下的 impl 文件夹新建 UserDaoImpl 实现类。
public class UserDaoImpl implements UserDao {
private SqlSessionFactory sqlSessionFactory;
public UserDaoImpl(SqlSessionFactory sqlSessionFactory) {
super();
this.sqlSessionFactory = sqlSessionFactory;
}
public void insertUser(User user) {
SqlSession sqlSession = sqlSessionFactory.openSession();
sqlSession.insert("insertUser", user);
sqlSession.commit();
sqlSession.close();
}
public void deleteUser(int id) {
SqlSession sqlSession = sqlSessionFactory.openSession();
User user = new User();
user.setId(id);
sqlSession.delete("deleteUser", user);
sqlSession.commit();
sqlSession.close();
}
public void updateUser(User user) {
SqlSession sqlSession = sqlSessionFactory.openSession();
sqlSession.update("updateUser", user);
sqlSession.commit();
sqlSession.close();
}
public User queryUserById(int id) {
SqlSession sqlSession = sqlSessionFactory.openSession();
User user = new User();
user.setId(id);
User userResult = sqlSession.selectOne("queryUserById", user);
sqlSession.close();
return userResult;
}
public List queryUserByName(String name) {
SqlSession sqlSession = sqlSessionFactory.openSession();
List userList = sqlSession.selectList("queryUserByName", name);
sqlSession.close();
return userList;
}
}
在 test 文件夹 新建 junit 测试类。
public class test {
private SqlSessionFactory sqlSessionFactory = null;
@Before
public void init() throws Exception{
// 创建SqlSessionFactoryBuilder对象
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
// 加载mybaits-config.xml配置文件
InputStream inputStream = Resources.getResourceAsStream("mybaits-config.xml");
// 创建SqlsessionFactory
this.sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
}
@Test
public void testInsert(){
// 创建DAO
UserDao userDao = new UserDaoImpl(this.sqlSessionFactory);
User user = new User(1, "123", "abc", "张三", 110);
userDao.insertUser(user);
}
@Test
public void testDelete(){
UserDao userDao = new UserDaoImpl(this.sqlSessionFactory);
userDao.deleteUser(1);
}
@Test
public void testUpdate(){
UserDao userDao = new UserDaoImpl(this.sqlSessionFactory);
User user = new User(1, "123", "abc", "李四", 110);
userDao.insertUser(user);
}
@Test
public void testQueryUserById(){
UserDao userDao = new UserDaoImpl(this.sqlSessionFactory);
User user = userDao.queryUserById(1);
System.out.println(user);
}
@Test
public void testQueryUserByName(){
UserDao userDao = new UserDaoImpl(this.sqlSessionFactory);
List userList = userDao.queryUserByName("张");
System.out.println(userList);
}
}