系列博文:  

    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

JavaWeb_(Mybatis框架)使用Mybatis对表进行增、删、改、查操作_二的更多相关文章

  1. 怎样从C#中打开数据库并进行 增 删 改 查 操作

    首先 在C#中引用数据库的操作! (因为我们用的是SQLserver数据库,所以是SqlClient) using System.Data.SqlClient; 1:要实现对数据库的操作,我们必须先登 ...

  2. iOS FMDB的使用(增,删,改,查,sqlite存取图片)

    iOS FMDB的使用(增,删,改,查,sqlite存取图片) 在上一篇博客我对sqlite的基本使用进行了详细介绍... 但是在实际开发中原生使用的频率是很少的... 这篇博客我将会较全面的介绍FM ...

  3. iOS sqlite3 的基本使用(增 删 改 查)

    iOS sqlite3 的基本使用(增 删 改 查) 这篇博客不会讲述太多sql语言,目的重在实现sqlite3的一些基本操作. 例:增 删 改 查 如果想了解更多的sql语言可以利用强大的互联网. ...

  4. 好用的SQL TVP~~独家赠送[增-删-改-查]的例子

    以前总是追求新东西,发现基础才是最重要的,今年主要的目标是精通SQL查询和SQL性能优化.  本系列主要是针对T-SQL的总结. [T-SQL基础]01.单表查询-几道sql查询题 [T-SQL基础] ...

  5. django ajax增 删 改 查

    具于django ajax实现增 删 改 查功能 代码示例: 代码: urls.py from django.conf.urls import url from django.contrib impo ...

  6. ADO.NET 增 删 改 查

    ADO.NET:(数据访问技术)就是将C#和MSSQL连接起来的一个纽带 可以通过ADO.NET将内存中的临时数据写入到数据库中 也可以将数据库中的数据提取到内存中供程序调用 ADO.NET所有数据访 ...

  7. MVC EF 增 删 改 查

    using System;using System.Collections.Generic;using System.Linq;using System.Web;//using System.Data ...

  8. MyBatis的配置与使用(增,删,改,查)

    ---恢复内容开始--- Mybatis入门介绍 一.MyBatis介绍 什么是MyBtis? MyBatis 是一个简化和实现了 Java 数据持久化层(persistence layer)的开源框 ...

  9. MongoDB增 删 改 查

    增 增加单篇文档 > db.stu.insert({sn:'001', name:'lisi'}) WriteResult({ "nInserted" : 1 }) > ...

随机推荐

  1. [javascript]localStorage和sessionStorage区别

    一.sessionStorage.localStorage.cookie可查看的位置,F12=>Application: 二.cookie .sessionStorage与localStorag ...

  2. Scala学习十六——XML处理

    一.本章要点 XML字面量<like>this</like>的类型为NodeSeq 可以在XML字面量中内嵌Scala代码 Node的child属性产出后代节点 Node的at ...

  3. JS笛卡尔积算法与多重数组笛卡尔积实现方法示例

    js 笛卡尔积算法的实现代码,据对象或者数组生成笛卡尔积,并介绍了一个javascript多重数组笛卡尔积的例子,以及java实现笛卡尔积的算法与实例代码. 一.javascript笛卡尔积算法代码 ...

  4. Python打

    .智能识别图片物体.这步是智能垃圾分类的魔法核心.原理是人工智能会根据打上标签的海量图片来识别新的图片所归属的分类标签.好奇的读者可能会问,我没学过深度学习啊?我也不会训练模型,怎么办? python ...

  5. JS 发送弹幕

    JS实现弹幕的发送 <div class="box1"> <div class="box2" style="width: 600px ...

  6. git本地仓库删除后从远程仓库更新代码

    1.$ ssh-agent bash 2.$ ssh-add ~/.ssh/id_rsa_hyp 添加ssh密匙 3.$ git fetch git@github.com:xxx.git master ...

  7. yum安装mysql-server

    yum install mysql-server mysql vi /etc/my.cnf [client] default-character-set=utf8 service mysqld sta ...

  8. SCU 4442 party 二分图最大点权独立集

    每个青蛙喝黑茶或者红茶或者都可以喝 M个矛盾关系 有矛盾的不能喝同种茶 但你可以花费Wi使得这个青蛙消除所有矛盾 把矛盾当作边 青蛙当作点 如果这两个青蛙只喝不同的一种茶就不建边 题目中保证了不存在奇 ...

  9. elasticsearch联想加搜索实例

    //搜索框具体的ajax如下: <form class="form-wrapper cf"> <img src="__PUBLIC__/Home/img ...

  10. python -m pip install [package] --no-deps

    python -m pip install [package]  --no-deps 有些 packages 会依赖一些其它的 package,当我们离线安装 whl 的时候,就无法联网下载依赖包,所 ...