JavaWeb_(Mybatis框架)使用Mybatis对表进行增、删、改、查操作_二

 

 

  系列博文:  

    JavaWeb_(Mybatis框架)JDBC操作数据库和Mybatis框架操作数据库区别_一    传送门

    JavaWeb_(Mybatis框架)使用Mybatis对表进行增、删、改、查操作_二        传送门

    JavaWeb_(Mybatis框架)Mapper动态代理开发_三                 传送门

    JavaWeb_(Mybatis框架)主配置文件介绍_四                     传送门

    JavaWeb_(Mybatis框架)输入和输出参数_五                   传送门

    JavaWeb_(Mybatis框架)关联查询_六传送门                   传送门

    JavaWeb_(Mybatis框架)动态sql_七传送门                   传送门

 

 

  原有基础上,创建HelloMyBatis.java、HelloMyBatis2.java、HelloMyBatis3.java、HelloMyBatis4.java、HelloMyBatis5.java分别为

    a)通过ID去查询一个用户

package com.Gary.test;

import java.io.IOException;
import java.io.InputStream;

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 org.junit.jupiter.api.Test;

import com.Gary.bean.User;

public class HelloMyBatis {

    @Test
    //入门程序,通过ID,查询用户
    public void Test1() throws IOException {
        //读取配置文件
        String resource = "sqlMapConfig.xml";
        
        InputStream in = Resources.getResourceAsStream(resource);
        
        //需要sqlSessionFactoryBuilder
        SqlSessionFactoryBuilder ssfb = new SqlSessionFactoryBuilder();
        
        //创建sqlSessionFactory
        SqlSessionFactory ssf = ssfb.build(in);
        
        //生产一个sqlSession
        SqlSession session = ssf.openSession();
        
        //操作数据库
        //参数1:要操作的sql语句   参数2:sql语句的参数
        User user = session.selectOne("UserMapper.selectUserById", 1);
        System.err.println(user);
        
        
    }
    
}
HelloMyBatis.java

 

    b)通过用户名模糊查找匹配的用户列表

package com.Gary.test;

import java.io.IOException;
import java.io.InputStream;
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 org.junit.jupiter.api.Test;

import com.Gary.bean.User;

public class HelloMyBatis2 {

    @Test
    //入门程序,通过ID,查询用户
    public void Test1() throws IOException {
        //读取配置文件
        String resource = "sqlMapConfig.xml";
        
        InputStream in = Resources.getResourceAsStream(resource);
        
        //需要sqlSessionFactoryBuilder
        SqlSessionFactoryBuilder ssfb = new SqlSessionFactoryBuilder();
        
        //创建sqlSessionFactory
        SqlSessionFactory ssf = ssfb.build(in);
        
        //生产一个sqlSession
        SqlSession session = ssf.openSession();
        
        //操作数据库
        //参数1:要操作的sql语句   参数2:sql语句的参数
        List<User> list = session.selectList("UserMapper.selectUserByName", "王");
    
        for(User u : list) {
            System.out.println(u);
        }
        
        
    }
    
}
HelloMyBatis2.java

 

    c)完成添加用户

package com.Gary.test;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
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 org.junit.jupiter.api.Test;

import com.Gary.bean.User;

public class HelloMyBatis3 {

    @Test
    //入门程序,通过ID,查询用户
    public void Test1() throws IOException {
        //读取配置文件
        String resource = "sqlMapConfig.xml";
        
        InputStream in = Resources.getResourceAsStream(resource);
        
        //需要sqlSessionFactoryBuilder
        SqlSessionFactoryBuilder ssfb = new SqlSessionFactoryBuilder();
        
        //创建sqlSessionFactory
        SqlSessionFactory ssf = ssfb.build(in);
        
        //生产一个sqlSession
        SqlSession session = ssf.openSession();
        
        //操作数据库
        //参数1:要操作的sql语句   参数2:sql语句的参数
        User user = new User();
        
        user.setU_username("Gary");
        user.setU_password("494949");
        user.setU_sex("1");
        user.setU_createTime(new Date());
        user.setU_cid(1);
        
        session.insert("UserMapper.insertUser", user);
        //提交事务
        session.commit();
    
        
        
    }
    
}
HelloMyBatis3.java

 

    d)修改用户

package com.Gary.test;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
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 org.junit.jupiter.api.Test;

import com.Gary.bean.User;

public class HelloMyBatis4 {

    @Test
    //入门程序,通过ID,查询用户
    public void Test1() throws IOException {
        //读取配置文件
        String resource = "sqlMapConfig.xml";
        
        InputStream in = Resources.getResourceAsStream(resource);
        
        //需要sqlSessionFactoryBuilder
        SqlSessionFactoryBuilder ssfb = new SqlSessionFactoryBuilder();
        
        //创建sqlSessionFactory
        SqlSessionFactory ssf = ssfb.build(in);
        
        //生产一个sqlSession
        SqlSession session = ssf.openSession();
        
        //操作数据库
        //参数1:要操作的sql语句   参数2:sql语句的参数
        User user = new User();
        user.setU_id(13);
        user.setU_username("修改后Gary");

        session.insert("UserMapper.updateUser", user);
        //提交事务
        session.commit();
    
        
        
    }
    
}
HelloMyBatis4.java

 

    e)根据id删除用户

package com.Gary.test;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
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 org.junit.jupiter.api.Test;

import com.Gary.bean.User;

public class HelloMyBatis5 {

    @Test
    //入门程序,通过ID,查询用户
    public void Test1() throws IOException {
        //读取配置文件
        String resource = "sqlMapConfig.xml";
        
        InputStream in = Resources.getResourceAsStream(resource);
        
        //需要sqlSessionFactoryBuilder
        SqlSessionFactoryBuilder ssfb = new SqlSessionFactoryBuilder();
        
        //创建sqlSessionFactory
        SqlSessionFactory ssf = ssfb.build(in);
        
        //生产一个sqlSession
        SqlSession session = ssf.openSession();
        
        //操作数据库
        //参数1:要操作的sql语句   参数2:sql语句的参数

        session.delete("UserMapper.deleteUserById", 13);
        //提交事务
        session.commit();
    
        
        
    }
    
}
HelloMyBatis5.java

 

<?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="UserMapper">
 
     <select id="selectUserById" parameterType="Integer" resultType="com.Gary.bean.User">
         select * from user where u_id = #{id}
     </select>
     
     <!-- #{}占位符 尽量使用#{}来解决问题 -->
     <!-- ${}字符串拼接   容易sql注入 (or 1 = 1) -->
     
     <!-- ${value}中间的字符串一定需要使用value -->
     <select id="selectUserByName" parameterType="String" resultType="com.Gary.bean.User">
         <!-- select * from user where u_username like '%${value}%' -->
              select * from user where u_username like "%"#{name}"%"
     </select>
     
     <!-- 添加用户 参数为全包名 -->
     <insert id="insertUser" parameterType="com.Gary.bean.User">
         insert into user values(null,#{u_username},#{u_password},#{u_sex},#{u_createTime},#{u_cid})
     </insert>
     
    <!-- 根据id修改username字段的语句 -->
     <update id="updateUser" parameterType="com.Gary.bean.User">
         update user set u_username = #{u_username} where u_id = #{u_id}
     </update>
     
     <!-- 根据id删除用户 -->
     <delete id="deleteUserById" parameterType="Integer">
         delete from user where u_id = #{id}
     </delete>
     
</mapper>


 
 
UserMapper.xml

 

  

 

实现过程

  添加log4j.properties用于测试时打印执行数据库的语句

# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
log4j.properties

 

  获取sqlSession

package com.Gary.test;

import java.io.IOException;
import java.io.InputStream;

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 org.junit.jupiter.api.Test;

public class HelloMyBatis {

    @Test
    //入门程序,通过ID,查询用户
    public void Test1() throws IOException {
        //读取配置文件
        String resource = "sqlMapConfig.xml";
        
        InputStream in = Resources.getResourceAsStream(resource);
        
        //需要sqlSessionFactoryBuilder
        SqlSessionFactoryBuilder ssfb = new SqlSessionFactoryBuilder();
        
        //创建sqlSessionFactory
        SqlSessionFactory ssf = ssfb.build(in);
        
        //生产一个sqlSession
        SqlSession session = ssf.openSession();
        
        //操作数据库
        //参数1:要操作的sql语句   参数2:sql语句的参数
        //session.select(statement, parameter);
        
        
        
    }
    
}
HelloMyBatis.java

 

  接下来的操作在UserMapper中书写sql语句并完成查询

 

一、通过ID去查找一个用户

 <mapper namespace="UserMapper">
     <select id="selectUserById" parameterType="Integer" resultType="com.Gary.bean.User">
         select * from user where u_id = #{id}
     </select>
 </mapper>

 

<?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="UserMapper">
     <select id="selectUserById" parameterType="Integer" resultType="com.Gary.bean.User">
         select * from user where u_id = #{id}
     </select>
 </mapper>
UserMapper.xml

 

  

 

  通过ID去查询一个用户

  //参数1:要操作的sql语句   参数2:sql语句的参数
  User user = session.selectOne("UserMapper.selectUserById", 1);

 

  

package com.Gary.test;

import java.io.IOException;
import java.io.InputStream;

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 org.junit.jupiter.api.Test;

import com.Gary.bean.User;

public class HelloMyBatis {

    @Test
    //入门程序,通过ID,查询用户
    public void Test1() throws IOException {
        //读取配置文件
        String resource = "sqlMapConfig.xml";
        
        InputStream in = Resources.getResourceAsStream(resource);
        
        //需要sqlSessionFactoryBuilder
        SqlSessionFactoryBuilder ssfb = new SqlSessionFactoryBuilder();
        
        //创建sqlSessionFactory
        SqlSessionFactory ssf = ssfb.build(in);
        
        //生产一个sqlSession
        SqlSession session = ssf.openSession();
        
        //操作数据库
        //参数1:要操作的sql语句   参数2:sql语句的参数
        User user = session.selectOne("UserMapper.selectUserById", 1);
        System.err.println(user);
        
        
    }
    
}
HelloMyBatis.java

 

 

二、通过用户名模糊查询用户列表

  查找数据库中名字带有王字的数据

     <!-- ${value}中间的字符串一定需要使用value -->
     <select id="selectUserByName" parameterType="String" resultType="com.Gary.bean.User">
         select * from user where u_username like '%${value}%'
     </select>

 

<?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="UserMapper">
 
     <select id="selectUserById" parameterType="Integer" resultType="com.Gary.bean.User">
         select * from user where u_id = #{id}
     </select>
     
     <!-- ${value}中间的字符串一定需要使用value -->
     <select id="selectUserByName" parameterType="String" resultType="com.Gary.bean.User">
         select * from user where u_username like '%${value}%'
     </select>

 </mapper>

 
 
UserMapper.xml

 

  

  

  通过字符串去进行模糊查询

  //操作数据库
  //参数1:要操作的sql语句   参数2:sql语句的参数
  List<User> list = session.selectList("UserMapper.selectUserByName", "王");

 

  

 

package com.Gary.test;

import java.io.IOException;
import java.io.InputStream;
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 org.junit.jupiter.api.Test;

import com.Gary.bean.User;

public class HelloMyBatis2 {

    @Test
    //入门程序,通过ID,查询用户
    public void Test1() throws IOException {
        //读取配置文件
        String resource = "sqlMapConfig.xml";
        
        InputStream in = Resources.getResourceAsStream(resource);
        
        //需要sqlSessionFactoryBuilder
        SqlSessionFactoryBuilder ssfb = new SqlSessionFactoryBuilder();
        
        //创建sqlSessionFactory
        SqlSessionFactory ssf = ssfb.build(in);
        
        //生产一个sqlSession
        SqlSession session = ssf.openSession();
        
        //操作数据库
        //参数1:要操作的sql语句   参数2:sql语句的参数
        List<User> list = session.selectList("UserMapper.selectUserByName", "王");
    
        for(User u : list) {
            System.out.println(u);
        }
        
        
    }
    
}
HelloMyBatis2.java

 

  注意:

    #{}占位符 尽量使用#{}来解决问题  使用占位符#{}中间的字符串一定要为value

    ${}字符串拼接   容易sql注入 (or 1 = 1)  

     <!-- ${value}中间的字符串一定需要使用value -->
     <select id="selectUserByName" parameterType="String" resultType="com.Gary.bean.User">
         <!-- select * from user where u_username like '%${value}%' -->
              select * from user where u_username like "%"#{name}"%"
     </select>

 

  

package com.Gary.test;

import java.io.IOException;
import java.io.InputStream;
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 org.junit.jupiter.api.Test;

import com.Gary.bean.User;

public class HelloMyBatis2 {

    @Test
    //入门程序,通过ID,查询用户
    public void Test1() throws IOException {
        //读取配置文件
        String resource = "sqlMapConfig.xml";
        
        InputStream in = Resources.getResourceAsStream(resource);
        
        //需要sqlSessionFactoryBuilder
        SqlSessionFactoryBuilder ssfb = new SqlSessionFactoryBuilder();
        
        //创建sqlSessionFactory
        SqlSessionFactory ssf = ssfb.build(in);
        
        //生产一个sqlSession
        SqlSession session = ssf.openSession();
        
        //操作数据库
        //参数1:要操作的sql语句   参数2:sql语句的参数
        List<User> list = session.selectList("UserMapper.selectUserByName", "王");
    
        for(User u : list) {
            System.out.println(u);
        }
        
        
    }
    
}
HelloMyBatis2.java

 

<?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="UserMapper">
 
     <select id="selectUserById" parameterType="Integer" resultType="com.Gary.bean.User">
         select * from user where u_id = #{id}
     </select>
     
     <!-- #{}占位符 尽量使用#{}来解决问题 -->
     <!-- ${}字符串拼接   容易sql注入 (or 1 = 1) -->
     
     <!-- ${value}中间的字符串一定需要使用value -->
     <select id="selectUserByName" parameterType="String" resultType="com.Gary.bean.User">
         <!-- select * from user where u_username like '%${value}%' -->
              select * from user where u_username like "%"#{name}"%"
     </select>

 </mapper>

 
 
UserMapper.xml

 

 

三、添加用户

   在UserMapper.xml中编写插入User对象

  <!-- 添加用户 参数为全包名 -->
  <insert id="insertUser" parameterType="com.Gary.bean.User">
    insert into user values(null,#{u_username},#{u_password},#{u_sex},#{u_createTime},#{u_cid})
  </insert>

 

<?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="UserMapper">
 
     <select id="selectUserById" parameterType="Integer" resultType="com.Gary.bean.User">
         select * from user where u_id = #{id}
     </select>
     
     <!-- #{}占位符 尽量使用#{}来解决问题 -->
     <!-- ${}字符串拼接   容易sql注入 (or 1 = 1) -->
     
     <!-- ${value}中间的字符串一定需要使用value -->
     <select id="selectUserByName" parameterType="String" resultType="com.Gary.bean.User">
         <!-- select * from user where u_username like '%${value}%' -->
              select * from user where u_username like "%"#{name}"%"
     </select>
     
     <!-- 添加用户 参数为全包名 -->
     <insert id="insertUser" parameterType="com.Gary.bean.User">
         insert into user values(null,#{u_username},#{u_password},#{u_sex},#{u_createTime},#{u_cid})
     </insert>

     
</mapper>


 
 
UserMapper.xml

 

  使用了session.insert()后别忘记提交事务!

   

package com.Gary.test;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
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 org.junit.jupiter.api.Test;

import com.Gary.bean.User;

public class HelloMyBatis3 {

    @Test
    //入门程序,通过ID,查询用户
    public void Test1() throws IOException {
        //读取配置文件
        String resource = "sqlMapConfig.xml";
        
        InputStream in = Resources.getResourceAsStream(resource);
        
        //需要sqlSessionFactoryBuilder
        SqlSessionFactoryBuilder ssfb = new SqlSessionFactoryBuilder();
        
        //创建sqlSessionFactory
        SqlSessionFactory ssf = ssfb.build(in);
        
        //生产一个sqlSession
        SqlSession session = ssf.openSession();
        
        //操作数据库
        //参数1:要操作的sql语句   参数2:sql语句的参数
        User user = new User();
        
        user.setU_username("Gary");
        user.setU_password("494949");
        user.setU_sex("1");
        user.setU_createTime(new Date());
        user.setU_cid(1);
        
        session.insert("UserMapper.insertUser", user);
        //提交事务
        session.commit();
    
        
        
    }
    
}
HelloMyBatis3.java

 

  

 

 

四、修改用户

  修改id为13的Gary的username字段

  在UserMapper.xml中编写修改User对象字段username

  <!-- 根据id修改username字段的语句 -->
  <update id="updateUser" parameterType="com.Gary.bean.User">
    update user set u_username = #{u_username} where u_id = #{u_id}
  </update>

 

<?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="UserMapper">
 
     <select id="selectUserById" parameterType="Integer" resultType="com.Gary.bean.User">
         select * from user where u_id = #{id}
     </select>
     
     <!-- #{}占位符 尽量使用#{}来解决问题 -->
     <!-- ${}字符串拼接   容易sql注入 (or 1 = 1) -->
     
     <!-- ${value}中间的字符串一定需要使用value -->
     <select id="selectUserByName" parameterType="String" resultType="com.Gary.bean.User">
         <!-- select * from user where u_username like '%${value}%' -->
              select * from user where u_username like "%"#{name}"%"
     </select>
     
     <!-- 添加用户 参数为全包名 -->
     <insert id="insertUser" parameterType="com.Gary.bean.User">
         insert into user values(null,#{u_username},#{u_password},#{u_sex},#{u_createTime},#{u_cid})
     </insert>
     
    <!-- 根据id修改username字段的语句 -->
     <update id="updateUser" parameterType="com.Gary.bean.User">
         update user set u_username = #{u_username} where u_id = #{u_id}
     </update>
     
</mapper>


 
 
UserMapper.xml

 

  将id为13的用户,名字修改成"修改后Gary"

  

 

package com.Gary.test;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
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 org.junit.jupiter.api.Test;

import com.Gary.bean.User;

public class HelloMyBatis4 {

    @Test
    //入门程序,通过ID,查询用户
    public void Test1() throws IOException {
        //读取配置文件
        String resource = "sqlMapConfig.xml";
        
        InputStream in = Resources.getResourceAsStream(resource);
        
        //需要sqlSessionFactoryBuilder
        SqlSessionFactoryBuilder ssfb = new SqlSessionFactoryBuilder();
        
        //创建sqlSessionFactory
        SqlSessionFactory ssf = ssfb.build(in);
        
        //生产一个sqlSession
        SqlSession session = ssf.openSession();
        
        //操作数据库
        //参数1:要操作的sql语句   参数2:sql语句的参数
        User user = new User();
        user.setU_id(13);
        user.setU_username("修改后Gary");

        session.insert("UserMapper.updateUser", user);
        //提交事务
        session.commit();
    
        
        
    }
    
}
HelloMybatis4.java

 

  

 

 

五、根据id删除用户

  删除id为13的Gary用户

  在UserMapper.xml中编写根据id删除用户

     <!-- 根据id删除用户 -->
     <delete id="deleteUserById" parameterType="Integer">
         delete from user where u_id = #{id}
     </delete>

 

<?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="UserMapper">
 
     <select id="selectUserById" parameterType="Integer" resultType="com.Gary.bean.User">
         select * from user where u_id = #{id}
     </select>
     
     <!-- #{}占位符 尽量使用#{}来解决问题 -->
     <!-- ${}字符串拼接   容易sql注入 (or 1 = 1) -->
     
     <!-- ${value}中间的字符串一定需要使用value -->
     <select id="selectUserByName" parameterType="String" resultType="com.Gary.bean.User">
         <!-- select * from user where u_username like '%${value}%' -->
              select * from user where u_username like "%"#{name}"%"
     </select>
     
     <!-- 添加用户 参数为全包名 -->
     <insert id="insertUser" parameterType="com.Gary.bean.User">
         insert into user values(null,#{u_username},#{u_password},#{u_sex},#{u_createTime},#{u_cid})
     </insert>
     
    <!-- 根据id修改username字段的语句 -->
     <update id="updateUser" parameterType="com.Gary.bean.User">
         update user set u_username = #{u_username} where u_id = #{u_id}
     </update>
     
     <!-- 根据id删除用户 -->
     <delete id="deleteUserById" parameterType="Integer">
         delete from user where u_id = #{id}
     </delete>
     
</mapper>


 
 
UserMapper.xml

 

  删除id为13的user对象

   

 

<?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="UserMapper">
 
     <select id="selectUserById" parameterType="Integer" resultType="com.Gary.bean.User">
         select * from user where u_id = #{id}
     </select>
     
     <!-- #{}占位符 尽量使用#{}来解决问题 -->
     <!-- ${}字符串拼接   容易sql注入 (or 1 = 1) -->
     
     <!-- ${value}中间的字符串一定需要使用value -->
     <select id="selectUserByName" parameterType="String" resultType="com.Gary.bean.User">
         <!-- select * from user where u_username like '%${value}%' -->
              select * from user where u_username like "%"#{name}"%"
     </select>
     
     <!-- 添加用户 参数为全包名 -->
     <insert id="insertUser" parameterType="com.Gary.bean.User">
         insert into user values(null,#{u_username},#{u_password},#{u_sex},#{u_createTime},#{u_cid})
     </insert>
     
    <!-- 根据id修改username字段的语句 -->
     <update id="updateUser" parameterType="com.Gary.bean.User">
         update user set u_username = #{u_username} where u_id = #{u_id}
     </update>
     
     <!-- 根据id删除用户 -->
     <delete id="deleteUserById" parameterType="Integer">
         delete from user where u_id = #{id}
     </delete>
     
</mapper>


 
 
HelloMyBatis5.java

 

  

 

 

 六、使用MyBatis开发简易Dao层

   实现根据用户id查询用户测试方法

   

 

  

package com.Gary.dao;

import com.Gary.bean.User;

public interface UserDao {

    //根据id查询用户
    public User getUserById(Integer id);
    
}
UserDao.java

 

package com.Gary.dao;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import com.Gary.bean.User;

public class UserDaoImpl implements UserDao{

    //sqlSession工厂
    private SqlSessionFactory ssf;
    
    //通过构造器给ssf赋值
    public UserDaoImpl(SqlSessionFactory ssf) {
        super();
        this.ssf = ssf;
    }

    @Override
    public User getUserById(Integer id) {
        //生产一个sqlSession
        SqlSession session = ssf.openSession();
                
        //操作数据库
        //参数1:要操作的sql语句   参数2:sql语句的参数
        return session.selectOne("UserMapper.selectUserById", id);
    
    }
}
UserDaoImpl.java

 

package com.Gary.dao;

import java.io.IOException;
import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import com.Gary.bean.User;

//取代Service层
public class UserDaoTest {

    //sqlSession工厂
    private static SqlSessionFactory ssf;

    //利用静态代码块获取sqlSessionFactory
    static {
        //读取配置文件
        String resource = "sqlMapConfig.xml";
        InputStream in;
        try {
            in = Resources.getResourceAsStream(resource);
            //需要sqlSessionFactoryBuilder
            SqlSessionFactoryBuilder ssfb = new SqlSessionFactoryBuilder();
            //创建sqlSessionFactory
            ssf = ssfb.build(in);
            in.close();
        } catch (IOException e) {
            
            e.printStackTrace();
        }

    }
    
    @Test
    public void DaoTest() {
        UserDao dao = new UserDaoImpl(ssf);
        //查询id为1的兑对象
        User user = dao.getUserById(1);
        System.out.println(user);
    }
    
}
UserDaoTest.java

 

 

 

 

 


posted @ 2019-12-03 20:11  Cynical丶Gary  阅读(833)  评论(0编辑  收藏  举报