++++++++++++++++++++++++++分割线++++++++++++++++++++++++++
注意namespace属性值为对应接口的全限定类名
<?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="org.rain.mybatis.mapper.SpecialSQLMapper">
</mapper>
++++++++++++++++++++++++++分割线++++++++++++++++++++++++++
List<User> getUserByLike(@Param("mohu") String mohu);
@Test
public void testGetUserByLike(){
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
SpecialSQLMapper specialSQLMapper = sqlSession.getMapper(SpecialSQLMapper.class);
List<User> users = specialSQLMapper.getUserByLike("a");
for (User user : users) {
System.out.println(user);
}
}
<!--List<User> getUserByLike(@Param("mohu") String mohu);-->
<select id="getUserByLike" resultType="User">
select * from t_user where username like '%${mohu}%'
</select>
注意:各种数据库的字符串拼接函数会不相同,本示例用的是Mysql的字符串拼接函数
<!--List<User> getUserByLike(@Param("mohu") String mohu);-->
<select id="getUserByLike" resultType="User">
select * from t_user where username like concat('%',#{mohu},"%")
</select>
注意:这种方式最常用;因为既没有SQL注入的问题,也没有不同数据库函数不一致问题
<!--List<User> getUserByLike(@Param("mohu") String mohu);-->
<select id="getUserByLike" resultType="User">
select * from t_user where username like "%"#{mohu}"%"
</select>
void deleteMoreUser(@Param("ids") String ids);
@Test
public void testDeleteMoreUser(){
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
SpecialSQLMapper specialSQLMapper = sqlSession.getMapper(SpecialSQLMapper.class);
specialSQLMapper.deleteMoreUser("7,8");
sqlSession.close();
}
注意:这里使用#{}占位符赋值会报错,因为ids是字符串,占位符赋值会自动为其添加单引号,而id字段是数值类型;
除了使用${}字符串拼接之外,批量删除还可以用Mybatis的动态SQL功能
<!--void deleteMoreUser(@Param("ids") String ids);-->
<delete id="deleteMoreUser">
delete from t_user where id in (${ids})
</delete>
+++++++++++++++++++++++++++++++++分割线+++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++分割线+++++++++++++++++++++++++++++++++
List<Map<String,Object>> getAllData(@Param("tableName") String tableName);
注意:这里使用#{}占位符赋值会报错,因为表名不能有单引号
<!--List<Map<String,Object>> getAllData(@Param("tableName") String tableName);-->
<select id="getAllData" resultType="Map">
select * from ${tableName}
</select>
@Test
public void testGetAllData(){
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
SpecialSQLMapper specialSQLMapper = sqlSession.getMapper(SpecialSQLMapper.class);
List<Map<String, Object>> t_user = specialSQLMapper.getAllData("t_user");
for (Map<String, Object> map : t_user) {
System.out.println(map);
}
sqlSession.close();
}
注意:当参数是实体类时,不需要使用@Param注解,因为可以通过实体类的属性名来获取参数值
void insertUser(User user);
注意:因为增删改的返回值是存放受影响的行数的,所以获取的自增主键只能存放在实体类的某个属性中
<!--void insertUser(User user);-->
<!--
useGeneratedKeys属性:设置当前的添加功能使用自增主键
keyProperty属性:设置获取的自增主键放在实体类的哪个属性中
-->
<insert id="insertUser" useGeneratedKeys="true" keyProperty="id">
insert into t_user values (null ,#{username},#{password},#{age},#{gender},#{email})
</insert>
@Test
public void testInsertUser(){
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
SpecialSQLMapper specialSQLMapper = sqlSession.getMapper(SpecialSQLMapper.class);
User user = new User();
user.setUsername("111");
user.setPassword("111");
user.setEmail("111@11.com");
user.setGender("1");
user.setAge(11);
System.out.println(user);
specialSQLMapper.insertUser(user);
System.out.println(user);
sqlSession.close();
}
注意执行添加功能后,id值的变化
手机扫一扫
移动阅读更方便
你可能感兴趣的文章