Mybatis基本配置以及CRUD
阿新 • • 發佈:2022-03-29
Mybatis框架學習
1.0、Mybatis常用結構
1.00、UserMapper 介面類
package com.chris.dao; import com.chris.pojo.User; import java.util.List; public interface UserMapper { List<User> getListUser(); User getUserById(int Id); int addUser(User user); int updateUser(User user); int delUser(int id); }
1.01、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.chris.dao.UserMapper"> <select id="getListUser" resultType="com.chris.pojo.User"> select * from chrisdb.zd_user </select> <select id="getUserById" parameterType="int" resultType="com.chris.pojo.User"> select * from chrisdb.zd_user where id = #{id} </select> <insert id="addUser" parameterType="com.chris.pojo.User"> insert into chrisdb.zd_user(id,name) values(#{id},#{name}) </insert> <update id="updateUser" parameterType="com.chris.pojo.User"> update chrisdb.zd_user set name = #{name} where id = #{id} </update> <delete id="delUser" parameterType="int" > DELETE from chrisdb.zd_user where id = #{id} </delete> </mapper>
1.02、連線mysql的配置檔案
存放目錄 \src\main\resources
<?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.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/chrisdb?useSSL=true&useUnicode=true&characterEncoding=UTF-8"/> <property name="username" value="root"/> <property name="password" value=""/> </dataSource> </environment> </environments> <mappers> <mapper resource="com/chris/dao/UserMapper.xml"/> </mappers> </configuration>
1.03、測試單元
package com.chris.dao;
import com.chris.pojo.User;
import com.chris.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.List;
public class UserDaoTest {
@Test
public void test() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> listUser = mapper.getListUser();
for (User user : listUser) {
System.out.println(user);
}
}
/***
* 根據ID查詢一個
*/
@Test
public void test022() {
SqlSession sqlSession = null;
try {
sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User userById = mapper.getUserById(1);
System.out.println(userById);
} finally {
if (sqlSession != null) {
sqlSession.close();
}
}
}
@Test
public void test023() {
SqlSession sqlSession = null;
try {
sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User users = new User(5, "趙六");
int i = mapper.addUser(users);
sqlSession.commit();
System.out.println(i);
} finally {
if (sqlSession != null) {
sqlSession.close();
}
}
}
@Test
public void test03() {
SqlSession sqlSession = null;
try {
sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User users = new User(5, "趙六");
int i = mapper.updateUser(users);
sqlSession.commit();
System.out.println(i);
} finally {
sqlSession.close();
}
}
@Test
public void test04() {
SqlSession sqlSession = null;
try {
sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
int i = mapper.delUser(5);
sqlSession.commit();
System.out.println(i);
} catch (Exception exception) {
System.out.println(exception.getMessage());
} finally {
sqlSession.close();
}
}
}