Mybatis的xml+接口方式实现crud功能
- User.java
package com.qfedu.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
private int uid;
private String username;
private String password;
private int age;
private String addr;
}
2.IUserDao.java
import com.qfedu.pojo.User;
import java.util.List;
public interface IUserDao {
/**
* 查询所有的User对象
* @return User对象的列表
*/
List<User> getAllUsers();
/**
* 新增一个User对象
* @param u 要新增的用户对象
* @return 受影响的行数
*/
int saveUser(User u);
}
3.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">
<!--
第二种实现mybatis的方式为:接口+xml方式
这种方式有特别的要求
1. namespace必须是接口的全路径
2. 每个节点的id必须是接口中的方法名
3. 该接口中的方法不允许重载,否则namespace+id将不唯一
4. 注意该接口中的增删改的方法的返回值,最好使用int
-->
<mapper namespace="com.qfedu.dao.IUserDao">
<!--
ID为接口中的方法名
-->
<select id="getAllUsers" resultType="User">
select * from user
</select>
<insert id="saveUser" >
insert into user values(null, #{username}, #{password}, #{age}, #{addr})
</insert>
<update id="updateUser">
update user set username = #{username}, password =#{password}, age=#{age}, addr = #{addr} where uid = #{uid}
</update>
</mapper>
4.SessionUtils.java
package com.qfedu.util;
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 java.io.IOException;
public class SessionUtils {
private static SqlSession mSession = null;
private static SqlSessionFactory mFactory = null;
static {
try {
mFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis.xml"));
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 获取SqlSession对象
* @return
*/
public static SqlSession getSession(){
mSession = mFactory.openSession(true);
return mSession;
}
/**
* 关闭SqlSession对象
* @param session 要关闭的SqlSession对象
*/
public static void closeSession(SqlSession session){
if(session != null){
session.close();
session = null;
}
}
}
5.Mybatis.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节点
里面有配置信息 分别是环境和映射
其中环境里有datasource,里面有我们熟悉的连接数据库的四个字符串
-->
<configuration>
<!--
引入db的配置文件信息,后面用到的四个连接字符串就可以直接使用 ${}的方式来动态引入
-->
<properties resource="db.properties">
</properties>
<!--
给当前mybatis项目添加日志功能,该STDOUT_LOGGING值的好处是不用添加第三方jar包就可以有日志的输出
-->
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<typeAliases>
<!--<typeAlias type="com.qfedu.pojo.Order" alias="abc" />-->
<package name="com.qfedu.pojo" />
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${user}"/>
<property name="password" value="${pass}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/qfedu/dao/UserMapper.xml"/>
</mappers>
</configuration>
6.TestUser.java
package com.qfedu.pojo;
import org.junit.Test;
public class TestUser {
@Test
public void testUser(){
User u = new User();
u.setUid(1);
u.setUsername("chenggang");
u.setPassword("888888");
u.setAddr("shenzhen");
u.setAge(18);
System.out.println(u);
}
}
7.TestUserDao.java
import com.qfedu.dao.IUserDao;
import com.qfedu.util.SessionUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.List;
public class TestUserDao {
private SqlSession session = null;
@Before
public void setUp(){
session = SessionUtils.getSession();
}
@After
public void tearDown(){
SessionUtils.closeSession(session);
}
@Test
public void testGetAllUsers(){
IUserDao mapper = session.getMapper(IUserDao.class);
List<User> users = mapper.getAllUsers();
for (User u : users) {
System.out.println(u);
}
}
@Test
public void testSaveUser(){
IUserDao userDao = session.getMapper(IUserDao.class);
User u = new User(17, "liuchang", "888888", 20, "guangzhou");
int result = userDao.saveUser(u);
System.out.println(result);
}
}