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
JavaWeb_(Mybatis框架)使用Mybatis对表进行增、删、改、查操作_二的更多相关文章
- 怎样从C#中打开数据库并进行 增 删 改 查 操作
首先 在C#中引用数据库的操作! (因为我们用的是SQLserver数据库,所以是SqlClient) using System.Data.SqlClient; 1:要实现对数据库的操作,我们必须先登 ...
- iOS FMDB的使用(增,删,改,查,sqlite存取图片)
iOS FMDB的使用(增,删,改,查,sqlite存取图片) 在上一篇博客我对sqlite的基本使用进行了详细介绍... 但是在实际开发中原生使用的频率是很少的... 这篇博客我将会较全面的介绍FM ...
- iOS sqlite3 的基本使用(增 删 改 查)
iOS sqlite3 的基本使用(增 删 改 查) 这篇博客不会讲述太多sql语言,目的重在实现sqlite3的一些基本操作. 例:增 删 改 查 如果想了解更多的sql语言可以利用强大的互联网. ...
- 好用的SQL TVP~~独家赠送[增-删-改-查]的例子
以前总是追求新东西,发现基础才是最重要的,今年主要的目标是精通SQL查询和SQL性能优化. 本系列主要是针对T-SQL的总结. [T-SQL基础]01.单表查询-几道sql查询题 [T-SQL基础] ...
- django ajax增 删 改 查
具于django ajax实现增 删 改 查功能 代码示例: 代码: urls.py from django.conf.urls import url from django.contrib impo ...
- ADO.NET 增 删 改 查
ADO.NET:(数据访问技术)就是将C#和MSSQL连接起来的一个纽带 可以通过ADO.NET将内存中的临时数据写入到数据库中 也可以将数据库中的数据提取到内存中供程序调用 ADO.NET所有数据访 ...
- MVC EF 增 删 改 查
using System;using System.Collections.Generic;using System.Linq;using System.Web;//using System.Data ...
- MyBatis的配置与使用(增,删,改,查)
---恢复内容开始--- Mybatis入门介绍 一.MyBatis介绍 什么是MyBtis? MyBatis 是一个简化和实现了 Java 数据持久化层(persistence layer)的开源框 ...
- MongoDB增 删 改 查
增 增加单篇文档 > db.stu.insert({sn:'001', name:'lisi'}) WriteResult({ "nInserted" : 1 }) > ...
随机推荐
- c#泛型约束(转载)
博客地址:https://www.cnblogs.com/zhengwk/p/5541921.html 六种类型的约束: T:结构 类型参数必须是值类型.可以指定除 Nullable 以外的任何值类型 ...
- luogu题解 P4092 【[HEOI2016/TJOI2016]树】树链剖分
题目链接: https://www.luogu.org/problemnew/show/P4092 瞎扯--\(O(Q \log^3 N)\)解法 这道先yy出了一个\(O(Q \log^3 N)\) ...
- Cron 表达式详解
Crontab Crontab简介 crontab命令常见于Unix和类Unix的操作系统之中,用于设置周期性被执行的指令.该命令从标准输入设备读取指令,并将其存放于"crontab&quo ...
- NodeJS express框架的使用
首先,可以通过npm或者淘宝镜像cnpm全局安装epress框架,这里不具体说了 npm install -g expressnpm install -g express-generator 新建一个 ...
- IDEA GIT 忽略文件
1.装插件 .igore 2.新建忽略文件格式 3.编辑忽略后缀文件 可以是文件夹 也可以是 具体文件类型
- 安全开发Java动态代理
关于安全开发的一些思考 之前面试某宝的时候,某人问过我,如果解决开发不懂安全的问题,就比如说SSRF,XEE这样的漏洞,如果一旦发生,应该如果立刻去响应,并帮助开发人员修复漏洞,难道写一个jar包?然 ...
- linux——实际工作中如何使用linux
实际工作中,linux系统都不会在我们自己的电脑上,linux系统安装在机房的服务器上,我们操作linux不可能跑到机房去,所以我们需要有一个工具,能在公司通过网络远程连接到机房的linux服务器上 ...
- 8.caffe:make_mean.sh( 数据平均化 )
个人实践代码如下: #!/usr/bin/env sh # Compute the mean image from the imagenet training lmdb # N.B. this is ...
- PAT乙级1038
题目链接 https://pintia.cn/problem-sets/994805260223102976/problems/994805284092887040 题解一 这份代码最后一个点会超时 ...
- modbus-RTU-crc16——c语言
为确保消息数据的完整性,除了验证消息CRC之外,建议实现检查串行端口(UART)成帧错误的代码.如果接收消息中的CRC与接收设备计算的CRC不匹配,则应忽略该消息.下面的C语言代码片段显示了如何使用逐 ...