publicclassSqlSessionUtil{privatestaticSqlSessionFactory sf;static{InputStream in =UserDaoImpl.class.getClassLoader().getResourceAsStream("MybatisConfig.xml");
sf =newSqlSessionFactoryBuilder().build(in);}publicstaticSqlSessiongetSession(){return sf.openSession();}}
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPEmapperPUBLIC"-//ibatis.apache.org//DTD Mapper 3.0//EN""http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd"><mappernamespace="userDao"><insertid="addUser"parameterType="com.dudu.mybatis.bean.User">
insert into
t_user(name,pwd,email,phone)
values(
#{name},#{pwd},#{email},#{phone}
)
</insert><updateid="updateUser"parameterType="com.dudu.mybatis.bean.User">
update
t_user
set
name=#{name},pwd=#{pwd},email=#{email},phone=#{phone}
where
id=#{id}
</update><deleteid="delUser">
delete
from
t_user
where
id=#{id}
</delete><selectid="getUser"resultType="com.dudu.mybatis.bean.User">
select * from t_user where id=#{id}
</select><selectid="getAll"resultType="com.dudu.mybatis.bean.User">
select * from t_user;
</select></mapper>
7.TextUser
packagetest;importjava.util.List;importorg.junit.Test;importcn.tedu.mybatis.bean.User;importcn.tedu.mybatis.dao.UserDao;importcn.tedu.mybatis.dao.UserDaoImpl;publicclassTestUser{@TestpublicvoidtestInsertUser(){UserDao userDao =newUserDaoImpl();User user =newUser();
user.setName("王飞");
user.setPwd("123456");
user.setEmail("138000@123");
user.setPhone("12380021452");
userDao.insertUser(user);}@TestpublicvoidtestDelUser(){UserDao userDao =newUserDaoImpl();
userDao.deleteUser(2);}@TestpublicvoidtestSelectUser(){UserDao userDao =newUserDaoImpl();System.out.println(userDao.selectById(1));}@TestpublicvoidtestGetUserAll(){UserDao userDao =newUserDaoImpl();List<User> list = userDao.selectAll();for(User user : list){System.out.println(user);}}}