package com.jsoft.test;
import com.jsoft.dao.UserMapper;
import com.jsoft.entity.User;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.InputStream;
import java.util.List;
public class UserMapperTest {
// 获取session
SqlSession session;
// 在Test的方法执行之前执行的方法
@Before
public void before(){
// 构建一个session工厂
// 加载mybatis的主配置文件
InputStream inputStream = UserMapperTest.class.getClassLoader().getResourceAsStream("mybatis-config.xml");
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
session = sqlSessionFactory.openSession();
}
@After
public void after(){
try {
// 事务提交
session.commit();
// session关闭
session.close();
}catch(Exception e){
e.printStackTrace();
// 事务回滚
session.rollback();
}
}
@Test
public void testFileAllUsersByUsername(){
UserMapper mapper = session.getMapper(UserMapper.class);
List<User> users = mapper.findAllUsersByUsername("'%jing%'");
System.out.println(users);
}
@Test
public void testFindAllUsersByPage(){
UserMapper mapper = session.getMapper(UserMapper.class);
List<User> users = mapper.findAllUsersByPage(0);
System.out.println(users);
}
@Test
public void testFindAllUsers(){
UserMapper mapper = session.getMapper(UserMapper.class);
List<User> users = mapper.findAllUsers();
System.out.println(users);
}
@Test
public void testUpdateUser(){
UserMapper mapper = session.getMapper(UserMapper.class);
int i = mapper.updateUser(new User(5, "jingtian", "520520"));
}
@Test
public void testSaveUser(){
UserMapper mapper = session.getMapper(UserMapper.class);
int i = mapper.saveUser(new User(null, "zhou", "125125"));
}
@Test
public void testDeleteUserById(){
//获取到接口的代理实现类
UserMapper mapper = session.getMapper(UserMapper.class);
// 调接口里的方法
int i = mapper.deleteUserById(1);
// 提交事务
session.commit();
System.out.println(i);
}
@Test
public void testSelectUserById(){
//获取到接口的代理实现类
UserMapper mapper = session.getMapper(UserMapper.class);
// 调接口里的方法
User user = mapper.selectUserById(1);
System.out.println(user);
}
}
我们执行DML语句的时候,我们得到了正确的返回值1,mybatis默认不会给我们自动提交事务。
什么时候需要处理事务?
查询?
不需要只要数据库中的数据发生变化,就需要控制事务,新增,修改,删除
在Mybatis的映射文件中,$和#的区别?
$底层使用的是Statement,拼串,SQL注入的问题,不安全
底层使用的是PreparedStatement,预编译,#相当于占位符
*
如果想要做模糊查询,在我们的Java代码层面去解决%的问题。
service层,这些事都交给我们的service层
配置UserMapper.xml
<?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">
<!--namespace:对应的mapper接口的全类名-->
<mapper namespace="com.jsoft.dao.UserMapper">
<insert id="saveUser" parameterType="com.jsoft.entity.User">
insert into user(username,password) values (#{username},#{password})
</insert>
<update id="updateUser" parameterType="com.jsoft.entity.User">
update user set username = #{username},password=#{password} where id = #{id}
</update>
<delete id="deleteUserById" parameterType="java.lang.Integer">
delete from user where id = #{id}
</delete>
<!--写sql语句-->
<!--
id:mapper方法中的方法名,如果是entity类型,需要写全类名
resultType:方法的返回值类型
#{id}:代表方法的入参,类似于之前的?占位符,后边可以给它赋值
parameterType:方法的入参类型,可以省略
Mybatis的底层使用的是PreparedStatement
动态SQL where,if标签
-->
<select id="selectUserById" resultType="com.jsoft.entity.User" parameterType="int">
select id,username,password from user where id=#{id}
</select>
<!-- 如果方法的返回值是集合,在映射xml中,resultType应该怎么写?-->
<select id="findAllUsers" resultType="com.jsoft.entity.User">
select id,username,password from user
</select>
<select id="findAllUsersByPage" resultType="com.jsoft.entity.User">
select id,username,password from user limit #{pageNum},2
</select>
<select id="findAllUsersByUsername" resultType="com.jsoft.entity.User">
select id,username,password from user where username like #{jing}
</select>
<!-- 多条件查询-->
<select id="selectUsers" resultType="com.jsoft.entity.User">
select id,username,password from user
where 1 = 1
<if test="id !=null">
and id=#{id}
</if>
<if test="username !=null and username != ''">
and username=#{username}
</if>
<if test="password !=null and password != '' ">
and password=#{password}
</if>
</select>
</mapper>
UserMapper.java
package com.jsoft.dao;
import com.jsoft.entity.User;
import java.util.List;
public interface UserMapper {
/*这个方法可以根据不同的条件来查询数据
* 如果有username,根据username去查
* 如果有password,根据password去查
* 如果两个都有,根据username和password去查*/
List<User> selectUsers(User user);
// 模糊查询
List<User> findAllUsersByUsername(String username);
// 分页查询
List<User> findAllUsersByPage(Integer pageNum);
// 查询所有
List<User> findAllUsers();
// 根据id查询用户
User selectUserById(Integer id);
// 根据id删除用户
int deleteUserById(Integer id);
// 修改用户
int updateUser(User user);
int saveUser(User user);
}
实体类User
package com.jsoft.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
//对应数据库的user表,需要序列化
//实体类:1.实现序列化接口2.所有的属性封装,私有化,提供共有的set,get方法,一定要有无参构造器!!!
@Data
//全参构造器
@AllArgsConstructor
//无参构造器
@NoArgsConstructor
public class User implements Serializable {
private static final long serialVersionUID = -128497944707546L;
private Integer id;
private String username;
private String password;
}
mybatis-config.xml
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- environments:环境们
配置数据库连接相关,可以配置多个数据库连接-->
<environments default="development">
<environment id="development">
<!-- 事务管理-->
<transactionManager type="JDBC"></transactionManager>
<!-- 数据源配置-->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1/ssm?useSSL=false&useUnicode=true&characterEncoding=utf-8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<!-- 注册各个映射文件-->
<mappers>
<mapper resource="mapper/UserMapper.xml"></mapper>
</mappers>
</configuration>
标签:username,功能,UserMapper,用法,session,Mybatis,import,password,id
From: https://www.cnblogs.com/369-1/p/16751159.html