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 }) > ...
随机推荐
- Tag Helper1
Tag Helpers是服务器段的C#代码,在Razor文件里,参与到创建和渲染HTML元素的过程 和HTML Helpers类似 跟HTML的命名规范一致 内置了很多Tag Helpers也可以自定 ...
- Oracle 分页语句
** 写法1 :采用 ROWNUM的伪列: --查询10到20之间的数据 -- SELECT * FORM ( -- SELECT * , ROWNUM rn FROM TABLE_NAME -- W ...
- Pattern Recognition and Machine Learning-02-1.0-Introduction
Introduction The problem of searching for patterns in data is a fundamental one and has a long and s ...
- luogu4302字符串折叠题解--区间DP
题目链接 https://www.luogu.org/problemnew/show/P4302 分析 很明显一道区间DP题,对于区间\([l,r]\)的字符串,如果它的字串是最优折叠的,那么它的最优 ...
- ES6模块化及优点,简单案例让你秒懂
模块化:模块是一个文件 好处: 1.减少命名冲突 2.避免引入时的层层依赖 3.可以提升执行效率 **第一种方法************ ...
- 红黑树和AVL树
在此之前,我没有了解过红黑树以及AVL tree,真是孤陋寡闻.如果你也在学习的话,我们一起进步. 如果,你很急,那么只看红色加粗即可. 1.红黑树(RB-tree) 红黑树是一种特殊的二叉搜索树,特 ...
- 4.性能下降原因和常见的Join查询
性能下降 SQL慢,执行时间长,等待时间长 1.查询语句写的烂 2.索引失效 单值索引失效 和 复合索引失效 3.关联查询太多join(设计缺陷或不得已的需求) 4.服务器调优及各个参数设置(缓冲.线 ...
- gulp die('click').live('click' composer
gulp die('click').live('click' composer packagist.org https://getcomposer.org/ 下载后 php composer.pha ...
- C#面向对象 (访问修饰符、封装、继承、多态)
先看一下创建一个新项目时的基本格式 using System; using System.Collections.Generic; using System.Linq; //引用的命名空间 using ...
- OpenCV入门学习资料汇总
OpenCV学习文档资料 OpenCV学习:1)OpenCV中文网站——http://wiki.opencv.org.cn/index.php/%E9%A6%96%E9%A1%B5 2)python实 ...