https://www.yiibai.com/mybatis/mybatis-curd.html
接口
package com.yiibai.mybatis.dao;import java.util.List;import org.apache.ibatis.annotations.Select;import com.yiibai.mybatis.models.User;
/*** * @author yiibai**/
public interface IUser {//@Select("select * from user where id= #{id}")//public User getUserByID(int id);public List<User> getUserList();public void insertUser(User user);public void updateUser(User user);public void deleteUser(int userId);public User getUser(int id);
}
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"><mapper namespace="com.yiibai.mybatis.dao.IUser"><select id="getUser" parameterType="int"resultType="com.yiibai.mybatis.models.User">SELECT *FROM USERWHERE id = #{userId}</select><insert id="insertUser" parameterType="User">INSERT INTO USER(name,dept, website,phone)VALUES(#{name}, #{dept}, #{website}, #{phone})</insert><select id="getUserList" resultType="com.yiibai.mybatis.models.User">SELECT * FROM USER</select><update id="updateUser" parameterType="User">UPDATE USERSETname=#{name},dept = #{dept},website = #{website},phone = #{phone}WHEREid =#{id}</update><delete id="deleteUser" parameterType="int">DELETE FROM USER WHERE id = #{id}</delete></mapper>
import java.io.Reader;
import java.text.MessageFormat;
import java.util.List;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 com.yiibai.mybatis.dao.IUser;
import com.yiibai.mybatis.models.User;public class Main {private static SqlSessionFactory sqlSessionFactory;private static Reader reader;static {try {reader = Resources.getResourceAsReader("config/Configure.xml");sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);} catch (Exception e) {e.printStackTrace();}}public static SqlSessionFactory getSession() {return sqlSessionFactory;}/*** @param args*/public static void main(String[] args) {// TODO Auto-generated method stubSqlSession session = sqlSessionFactory.openSession();try {//sqlSessionFactory.getConfiguration().addMapper(IUser.class);//User user = (User) session.selectOne( "com.yiibai.mybatis.models.UserMapper.getUserByID", 1);// 用户数据列表getUserList();// 插入数据// testInsert();// 更新用户//testUpdate();// 删除数据//testDelete();} finally {session.close();}}//public static void testInsert(){try{// 获取Session连接SqlSession session = sqlSessionFactory.openSession();// 获取MapperIUser userMapper = session.getMapper(IUser.class);System.out.println("Test insert start...");// 执行插入User user = new User();user.setId(0);user.setName("Google");user.setDept("Tech");user.setWebsite("http://www.google.com");user.setPhone("120");userMapper.insertUser(user);// 提交事务session.commit();// 显示插入之后User信息System.out.println("After insert");getUserList();System.out.println("Test insert finished...");}catch (Exception e){e.printStackTrace();}}// 获取用户列表public static void getUserList() {try {SqlSession session = sqlSessionFactory.openSession();IUser iuser = session.getMapper(IUser.class);// 显示User信息System.out.println("Test Get start...");printUsers(iuser.getUserList());System.out.println("Test Get finished...");} catch (Exception e) {e.printStackTrace();}}public static void testUpdate(){try{SqlSession session = sqlSessionFactory.openSession();IUser iuser = session.getMapper(IUser.class);System.out.println("Test update start...");printUsers(iuser.getUserList());// 执行更新User user = iuser.getUser(1);user.setName("New name");iuser.updateUser(user);// 提交事务session.commit();// 显示更新之后User信息System.out.println("After update");printUsers(iuser.getUserList());System.out.println("Test update finished...");}catch (Exception e){e.printStackTrace();}}// 删除用户信息public static void testDelete(){try{SqlSession session = sqlSessionFactory.openSession();IUser iuser = session.getMapper(IUser.class);System.out.println("Test delete start...");// 显示删除之前User信息System.out.println("Before delete");printUsers(iuser.getUserList());// 执行删除iuser.deleteUser(2);// 提交事务session.commit();// 显示删除之后User信息System.out.println("After delete");printUsers(iuser.getUserList());System.out.println("Test delete finished...");}catch (Exception e){e.printStackTrace();}}/*** * 打印用户信息到控制台* * @param users*/private static void printUsers(final List<User> users) {int count = 0;for (User user : users) {System.out.println(MessageFormat.format("============= User[{0}]=================", ++count));System.out.println("User Id: " + user.getId());System.out.println("User Name: " + user.getName());System.out.println("User Dept: " + user.getDept());System.out.println("User Website: " + user.getWebsite());}}
}