接上一节:Mybatis入门:(2)接口方式编程
一、工程结构:
二、变动的文件
1、User.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">
<!--
此时这个namespace的值不能随便写,一定要是IUserDao的全路径
-->
<mapper namespace="com.linbilin.mybatis.dao.IUserDao">
<select id="getUserByID" parameterType="int" resultType="User">
select * from `user` where id = #{id}
</select>
<resultMap type="User" id="userListResult">
<id column="id" property="id"/>
<result column="name" property="name"/>
<result column="dept" property="dept"/>
<result column="website" property="website"/>
<result column="phone" property="phone"/>
</resultMap>
<select id="queryUsers" resultMap="userListResult">
select * from user
</select>
<!--
seGeneratedKeys设置 为"true"表示要myBatis获取由数据库自动生成的主键,
keyProperty="id"指定把获取到的主键值注入到User的id属性。
如果要用这两个属性的话,建表的时候需要把id字段设置为自增
-->
<insert id="addUser" parameterType="User" useGeneratedKeys="true" keyProperty="id">
INSERT INTO user(`name`,`dept`,`website`,`phone`)
VALUES
(#{name},#{dept},#{website},#{phone})
</insert>
<update id="updateUser" parameterType="User">
UPDATE `user`
SET `name` = #{name},
`dept` = #{dept},
`website` = #{website},
`phone` = #{phone}
WHERE `id` =#{id};
</update>
<delete id="deleteUser" parameterType="int">
delete from user where id=#{id}
</delete>
</mapper>
2、MyTest
package com.linbilin.mybatis.test;
import java.util.ArrayList;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import com.linbilin.mybatis.dao.IUserDao;
import com.linbilin.mybatis.models.User;
import com.linbilin.mybatis.utils.MybatisUtils;
public class MyTest {
public static void main(String[] args) {
//getUserByID(1);
User user=new User();
user.setDept("销售");
user.setName("陈");
user.setPhone("17733456875");
user.setWebsite("http://www.baidu.com");
addUser(user);
User user2=new User();
user2.setId(2);
user2.setDept("销售");
user2.setName("陈栋");
user2.setPhone("17733456875");
user2.setWebsite("http://www.baidu.com");
//updateUser(user2);
queryUsers();
//deleteUser(7);
queryUsers();
}
/**
* 根据id查询用户
*
* @param id
* @return
*/
public static User getUserByID(int id) {
SqlSession session = MybatisUtils.getSession().openSession();
User user = null;
try {
IUserDao userDao = session.getMapper(IUserDao.class);
user = userDao.getUserByID(id);
if (user != null) {
String userInfo = user.toString();
System.out.println(userInfo);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
session.close();// 关闭session,释放资源
}
return user;
}
/**
* 查询用户列表
*
* @return
*/
public static List<User> queryUsers() {
SqlSession session = MybatisUtils.getSession().openSession();
List<User> userList = new ArrayList<User>();
try {
IUserDao userDao = session.getMapper(IUserDao.class);
userList = userDao.queryUsers();
if (userList != null && !userList.isEmpty()) {
for (User user : userList) {
String userInfo = user.toString();
System.out.println(userInfo);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
session.close();// 关闭session,释放资源
}
return userList;
}
/**
* 添加用户
* @param user
*/
public static void addUser(User user){
SqlSession session = MybatisUtils.getSession().openSession();
try {
IUserDao userDao = session.getMapper(IUserDao.class);
userDao.addUser(user);
session.commit();//提交
System.out.println("添加了"+user.getName()+"的新数据!");
} catch (Exception e) {
e.printStackTrace();
} finally {
session.close();// 关闭session,释放资源
}
}
public static void updateUser(User user){
SqlSession session = MybatisUtils.getSession().openSession();
try {
IUserDao userDao = session.getMapper(IUserDao.class);
userDao.updateUser(user);
session.commit();//提交
System.out.println("更新了ID位"+user.getDept()+"的数据!");
} catch (Exception e) {
e.printStackTrace();
} finally {
session.close();// 关闭session,释放资源
}
}
/**
* 根据ID删除数据
* @param id
*/
public static void deleteUser(int id){
SqlSession session = MybatisUtils.getSession().openSession();
try {
IUserDao userDao = session.getMapper(IUserDao.class);
userDao.deleteUser(id);
session.commit();//提交
System.out.println("删除了ID为"+id+"的数据!");
} catch (Exception e) {
e.printStackTrace();
} finally {
session.close();// 关闭session,释放资源
}
}
}
3、IUserDao
package com.linbilin.mybatis.dao;
import java.util.List;
import com.linbilin.mybatis.models.User;
public interface IUserDao {
/**
* 根据id查询用户
* @param id
* @return
*/
public User getUserByID(int id);
/**
* 查询用户列表
* @return
*/
public List<User> queryUsers();
/**
* 添加用户
* @param user
*/
public void addUser(User user);
/**
* 更新数据
* @param user
*/
public void updateUser(User user);
/**
* 根据id删除用户
* @param id
*/
public void deleteUser(int id);
}
三、附件