第一个Mybatis程序
核心配置文件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>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/db4?useSSL=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/dao/UserMapper.xml"/>
</mappers>
</configuration>
编写mybatis工具类
public class MybatisUtils {
private static SqlSessionFactory sqlSessionFactory;
static {
try {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSession getSqlSession() {
return sqlSessionFactory.openSession();
}
}
编写代码
public class User {
private int id;
private String username;
private String password;
}
//重写方法toString方法等
public interface UserMapper {
List<User> getUserList();
}
- 接口实现类Mapper配置文件UserMapper.xml
<?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="com.dao.UserMapper">
<select id="getUserList" resultType="com.pojo.User">
select * from db4.user
</select>
</mapper>
public class test {
@Test
public void test1(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
List<User> userList = userMapper.getUserList();
for (User user : userList) {
System.out.println(user);
}
sqlSession.close();
}
}
<build>
<resources>
<resource>
<directory>src/main/resources/</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource> </resources>
</build>
CRUD
<?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="com.dao.UserMapper">
<select id="getUserList" resultType="com.pojo.User">
select *
from db4.user
</select>
<select id="getUserById" parameterType="int" resultType="com.pojo.User">
select *
from db4.user
where id = #{id}
</select>
<select id="addUser" parameterType="com.pojo.User">
insert into db4.user (id, username, password)
values (#{id}, #{username}, #{password})
</select>
<select id="updateUser" parameterType="com.pojo.User">
update db4.user
set username = #{username},
password = #{password}
where id = #{id};
</select>
<select id="deleteUser" parameterType="int">
delete
from db4.user
where id = #{id}
</select>
</mapper>
public interface UserMapper {
List<User> getUserList();
User getUserById(int id);
void addUser(User user);
void updateUser(User user);
void deleteUser(int id);
}
public class test {
@Test
public void getUserList_test(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
List<User> userList = userMapper.getUserList();
for (User user : userList) {
System.out.println(user);
}
sqlSession.close();
}
@Test
public void getUserById_test(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
User user = userMapper.getUserById(1);
System.out.println(user);
sqlSession.close();
}
@Test
public void addUser_test(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
userMapper.addUser(new User(66,"zhang", "7777"));
sqlSession.commit();
sqlSession.close();
}
@Test
public void updateUser_test(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
userMapper.updateUser(new User(2,"oooo", "000000"));
sqlSession.commit();
sqlSession.close();
}
@Test
public void deleteUser_test(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
userMapper.deleteUser(10);
sqlSession.commit();
sqlSession.close();
}
}
mybatis入门及CRUD