Mybatis初学习
1.导入maven mybatis包依赖
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
2.在resources创建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/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=true"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
</configuration>
在xml文件中,&符号要用&代替
3.创建mybatis工具类,获取SqlSession
public class MybatisUtils {
private static SqlSessionFactory sqlSessionFactory = null;
static{
String resource = "mybatis-config.xml";
InputStream inputStream = null;
try {
inputStream = Resources.getResourceAsStream(resource);
} catch (IOException e) {
e.printStackTrace();
}
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
}
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession();
}
}
4.创建user实体类
public class User {
private int id;
private String name;
private String pwd;
public User() {
}
public User(int id, String name, String pwd) {
this.id = id;
this.name = name;
this.pwd = pwd;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", pwd='" + pwd + '\'' +
'}';
}
}
5.创建UserMapper接口,即是原来的dao接口
public interface UserMapper {
List<User> getUserList();
User getUser(int id);
int insertUser(User user);
int updateUser(User user);
int deleteUser(int id);
}
6.创建UserMapper对应的xml配置文件,即是原来UserDao接口的实现类
<?xml version="1.0" encoding="GBK" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.sise.dao.UserMapper">
<select id="getUserList" resultType="com.sise.entity.User">
select * from `user`
</select>
<select id="getUser" resultType="com.sise.entity.User">
select * from `user` where `id`=#{id}
</select>
<insert id="insertUser" parameterType="com.sise.entity.User">
insert into `user` value (#{id},#{name},#{pwd})
</insert>
<update id="updateUser" parameterType="com.sise.entity.User">
update `user` set `name`=#{name},`pwd`=#{pwd} where `id`=#{id}
</update>
<delete id="deleteUser" parameterType="int">
delete from `user` where `id`=#{id}
</delete>
</mapper>
注意点:有中文注释可能会报错,要设置encoding=“GBK”
7.在mybatis-config.xml添加mapper配置
<mappers>
<mapper resource="com/sise/dao/UserMapper.xml"/>
</mappers>
resource的路径要用/,如果用com.sise.dao.UserMapper.xml会报错找不到文件
8.编写测试类
public class UserMapperTest {
@Test
public void getUserList(){
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 getUser(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User user = mapper.getUser(1);
System.out.println(user);
sqlSession.close();
}
@Test
public void insertUser(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
mapper.insertUser(new User(4,"赵六","123456"));
sqlSession.commit();
sqlSession.close();
}
@Test
public void updateUser(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
mapper.updateUser(new User(4,"秦八","123456"));
sqlSession.commit();
sqlSession.close();
}
@Test
public void deleteUser(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
mapper.deleteUser(4);
sqlSession.commit();
sqlSession.close();
}
}
|