Mybatis -- Mybatis注解开发:基本开发
Posted CodeJiao
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mybatis -- Mybatis注解开发:基本开发相关的知识,希望对你有一定的参考价值。
01:Mybatis – Mybatis注解开发:基本开发
02:Mybatis – MyBatis的注解实现复杂映射开发
说明:
我在mybatis核心配置文件配置了包别名。
<!-- 别名-->
<typeAliases>
<package name="com.tian.pojo"/>
</typeAliases>
我用到的依赖
pom.xml
<dependencies>
<!-- mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.25</version>
</dependency>
<!-- mybatis依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<!-- 单元测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
<!-- 日志打印-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
1. Mybatis注解开发
1.1 示例:User表基本操作
Mapper接口:
UserMapper.java
package com.itheima.mapper;
import com.itheima.domain.User;
import org.apache.ibatis.annotations.*;
import java.util.List;
public interface UserMapper {
@Insert("insert into user values(#{id},#{username},#{password},#{birthday})")
public void save(User user);
@Update("update user set username=#{username},password=#{password} where id=#{id}")
public void update(User user);
@Delete("delete from user where id=#{id}")
public void delete(int id);
@Select("select * from user where id=#{id}")
public User findById(int id);
@Select("select * from user")
public List<User> findAll();
}
修改MyBatis的核心配置文件
我们使用了注解替代的映射文件,所以我们只需要加载使用了注解的Mapper接口即可。
测试代码:
MyBatisTest.java
package com.itheima.test;
import com.itheima.domain.User;
import com.itheima.mapper.UserMapper;
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.List;
public class MyBatisTest {
private UserMapper mapper;
SqlSession sqlSession;
@Before // 在测试方法前执行
public void before() throws IOException {
InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
sqlSession = sqlSessionFactory.openSession(true);
mapper = sqlSession.getMapper(UserMapper.class);
}
@Test
public void testSave() {
User user = new User();
user.setUsername("tom");
user.setPassword("abc");
mapper.save(user);
}
@Test
public void testUpdate() {
User user = new User();
user.setId(18);
user.setUsername("lucy");
user.setPassword("123");
mapper.update(user);
}
@Test
public void testDelete() {
mapper.delete(18);
}
@Test
public void testFindById() {
User user = mapper.findById(2);
System.out.println(user);
}
@Test
public void testFindAll() {
List<User> all = mapper.findAll();
for (User user : all) {
System.out.println(user);
}
}
@After // 在测试方法后执行
public void closeResource() {
sqlSession.close();
}
}
以上是关于Mybatis -- Mybatis注解开发:基本开发的主要内容,如果未能解决你的问题,请参考以下文章