定义类型:
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 + '\'' +
'}';
}
}
核心配置文件:
<?xml version="1.0" encoding="UTF8" ?>
<!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/db1?characterEncoding=utf-8" />
<property name="username" value="root" />
<property name="password" value="1217" />
</dataSource>
</environment>
</environments>
<!--每一个Mapper.XML都需要在Mybatis核心配置文件中注册!-->
<mappers>
<mapper resource="com/dao/UserMapper.xml" />
</mappers>
</configuration>
接口:
public interface UserDao {
//查询全部用户
List<User> getUserList();
//根据ID查询用户
User getUserById(int id);
//增加用户insert
int addUser(User user);
//删除用户
int deleteUser(int id);
//更新用户
int updateUser(User user);
}
UserMapper.xml
<?xml version="1.0" encoding="UTF8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace=绑定一个对应的Dao/Mapper接口-->
<mapper namespace="com.dao.UserDao">
<!--resultType表示查询返回类型,parameterType表示参数类型-->
<select id="getUserList" resultType="com.pojo.User">
select * from db1.user
</select>
<select id="getUserById" resultType="com.pojo.User" parameterType="int">
select * from db1.user where id= #{id}
</select>
<insert id="addUser" parameterType="com.pojo.User">
insert into db1.user (id,name,pwd) values (#{id},#{name },#{pwd});
</insert>
<update id="updateUser" parameterType="com.pojo.User">
update db1.user set name =#{name},pwd=#{pwd} where id=#{id};
</update>
<delete id="deleteUser" parameterType="int">
delete from db1.user where id=#{id};
</delete>
<!--insert,update,delete,这三条语句没有返回值,所以不需要加入resultType,所以在接口里定义方法时,方法类型为int型-->
</mapper>
工具类:
public class MybatisUtils {
private static SqlSessionFactory sqlSessionFactory;
static {
try {
//使用mybatis第一步、获取sqlSessionFactory对象
String resource = "mybatis-Config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
//既然有了 SqlSessionFactory,顾名思义,我们就可以从中获得 SqlSession 的实例了。
// SqlSession 完全包含了面向数据库执行 SQL 命令所需的所有方法。
// 你可以通过 SqlSession 实例来直接执行已映射的 SQL 语句。
public static SqlSession getSqlSession() {
SqlSession sqlSession = sqlSessionFactory.openSession(); //也可以简化写成return sqlSessionFactory.openSession();
return sqlSession;
}
}
测试类:
public class Userdaotest {
@Test
public void test(){
//第一步:获得SqlSession对象
SqlSession sqlSession = MybatisUtils.getSqlSession();
//该代码结构可以保证sqlSession被正常关闭
/* try {
}catch (){
}finally {
}*/
try{
//方式一:getMapper
UserDao userDao =sqlSession.getMapper(UserDao.class);
List<User> userList=userDao.getUserList();
//方式二:不建议使用
//List<User> userList = sqlSession.selectList("com.fan.dao.UserDao.getUserList")
for (User user:userList){
System.out.println(user);
}
}catch (Exception e){
e.printStackTrace();
}finally {
//关闭SqlSession
sqlSession.close();
}
}
@Test
public void getUserById(){
SqlSession sqlSession=MybatisUtils.getSqlSession();
UserDao mapper = sqlSession.getMapper(UserDao.class);
User user = mapper.getUserById(1);
System.out.println(user);
sqlSession.close();
}
@Test
public void addUser(){
SqlSession sqlSession=MybatisUtils.getSqlSession();
UserDao mapper = sqlSession.getMapper(UserDao.class);
int addUser = mapper.addUser(new User(4, "嘻嘻", "555"));
if (addUser>0){
System.out.println("添加成功");
}
//增删改需要提交事务
sqlSession.commit();
sqlSession.close();
}
@Test
public void deleteUser(){
SqlSession sqlSession=MybatisUtils.getSqlSession();
UserDao mapper = sqlSession.getMapper(UserDao.class);
mapper.deleteUser(4);
sqlSession.commit();
sqlSession.close();
}
@Test
public void updateUser(){
SqlSession sqlSession=MybatisUtils.getSqlSession();
UserDao mapper = sqlSession.getMapper(UserDao.class);
mapper.updateUser(new User(3,"嘻嘻嘻","666"));
sqlSession.commit();
sqlSession.close();
}
}