//dao层
package com.csdn.dao;
import com.csdn.util.DBUtil;
import org.apache.ibatis.session.SqlSession;
import java.io.IOException;/**
* @ClassName
* @Description TODO
* @Author sudi
* @Date 2020/5/2 14:09
*/
public class UserDao {
public voiddeleteUserById(int id) throws IOException {
SqlSession sqlSession = null;
try {
sqlSession = new DBUtil().getSqlSession();
sqlSession.delete("deleteById", id);
sqlSession.commit();} finally {if(sqlSession != null){
sqlSession.close();}}}
public staticvoidmain(String[] args) throws IOException {
UserDao userDao = new UserDao();
userDao.deleteUserById(1);}}
//子配置文件层<?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="User"><!-- id = #{} 括号中放啥都行--><delete id="deleteById">
delete from t_person where id = #{id}</delete></mapper>
mybatis原始方法的增
/**
* 插入用户方法
* @param id int
* @param name String
* @param password String
*/
public voidinsertUser(int id, String name, String password) throws IOException {
SqlSession sqlSession = null;
try {// 1、获取sqlSession实例 ctrl + alt + v
sqlSession = new DBUtil().getSqlSession();// 2、调用sqlSession的insert方法
sqlSession.insert("insertUser", new User(id, name, password));// 3、提交
sqlSession.commit();} finally {// 4、关闭资源if(sqlSession != null){
sqlSession.close();}}}/**
* 插入方法
* @param user User
*/
public voidinsertUserByUser(User user) throws IOException {
SqlSession sqlSession = null;
try {// 1、获取sqlSession实例 ctrl + alt + v
sqlSession = new DBUtil().getSqlSession();// 2、调用sqlSession的insert方法
sqlSession.insert("insertUserByUser", user);// 3、提交
sqlSession.commit();} finally {// 4、关闭资源if(sqlSession != null){
sqlSession.close();}}}
//子配置文件层<insert id="insertUser" parameterType="com.colin.bean.User">
insert into user values(#{id}, #{name}, #{password})</insert><insert id="insertUserByUser">
insert into user values(#{id}, #{name}, #{password})</insert>
注意:当有多个参数传进来的时候,子配置文件中i insert into user values(#{id}, #{name}, #{password}) 的传入参数名字必须与User里边的成员变量名字一样。
mybatis原始方法的改
/**
* 修改用户
* @param user User
*/
public voidupdateUserByUser(User user) throws IOException {
SqlSession sqlSession = null;
try {
sqlSession = new DBUtil().getSqlSession();
sqlSession.update("updateByUser", user);
sqlSession.commit();} finally {if(sqlSession != null){
sqlSession.close();}}}//Map方法在主方法实现
public voidupdateUserByMap(Map<String, Object> map) throws IOException {
SqlSession sqlSession = null;
try {
sqlSession = new DBUtil().getSqlSession();
sqlSession.update("updateUserByMap", map);
sqlSession.commit();} finally {if(sqlSession != null){
sqlSession.close();}}}
// 子配置文件<update id="updateByUser">
update user set name = #{name}, password = #{password} where id = #{id}</update><update id="updateUserByMap">
update user set password = #{password} where id = #{id}</update>
mybatis原始方法的查
//dao层// 查询
public User selectUserById(int id) throws IOException {
SqlSession sqlSession = null;
try {// 1、获取sqlSession实例
sqlSession = new DBUtil().getSqlSession();// 2、调用selectOne方法
User user = sqlSession.selectOne("selectUserById", id);return user;} finally {if(sqlSession != null){
sqlSession.close();}}}// 注意,查询所有的子配置文件中的resultType属性放的值是泛型的类型
public List<User>selectAll() throws IOException {
SqlSession sqlSession = null;
try {
sqlSession = new DBUtil().getSqlSession();
List<User> list = sqlSession.selectList("selectAll");return list;} finally {if(sqlSession != null){
sqlSession.close();}}}
//子配置文件<select id="selectUserById" resultType="com.colin.bean.User">
select id, name, password from user where id = #{fangshadouxing}</select><select id="selectAll" resultType="com.colin.bean.User">
select id, name, password from user
</select>