mybatis 取查询值_Mybatis --- 映射文件、参数处理、参数值的获取、select元素
這樣就可以在insert函數(shù)中獲取新添加的用戶的 id主鍵,否則獲取不到
select * from student where id = #{id}
insert into student(name,password,email) values(#{name},#{password},#{email})
編寫測試單元:
private EmployeeMapper mapper = null;
private SqlSession session = null;
@Before
public void testBefore(){
//1.獲取sqlSessionFactory對象
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
//2.利用sqlSessionFactory創(chuàng)建一個session對象,表示和數(shù)據(jù)庫的一次會話
session = sqlSessionFactory.openSession();
//3.用session對象獲取mapper接口的代理對象
//因為sql映射文件給相應(yīng)的接口創(chuàng)建了一個代理對象,所以mapper接口類不需要實現(xiàn)類
mapper = session.getMapper(EmployeeMapper.class);
}
@Test
public void testSelect(){
mapper = session.getMapper(EmployeeMapper.class);
//4.通過mapper接口的代理對象就可以對數(shù)據(jù)庫進(jìn)行增刪改查操作
Employee employee = mapper.getEmployeeById(4);
System.out.println(employee);
}
@Test
public void testInsert(){
Employee emp = new Employee("zhangsan", "1234567", "zhangsan@q.com");
mapper.insertEmp(emp);
int id = emp.getId();
System.out.println(id);
}
@After
public void testAfter(){
//增刪改需要提交事務(wù)
session.commit();
session.close();
}
//@Before、@After自動在@Test之前和之后運行
//查詢不需要提交事務(wù),增刪改都需要提交事務(wù)
2.獲取自增主鍵值【當(dāng)向數(shù)據(jù)庫中插入一條數(shù)據(jù)的時候,默認(rèn)是拿不到主鍵的值的, 需要設(shè)置如下兩個屬性才可以拿到主鍵值!】
insert into tbl_employee(last_name,email,gender) values(#{lastName},#{gender},#{email})
3.SQL節(jié)點:
1).可以用于存儲被重用的SQL片段
2).在sql映射文件中,具體使用方式如下:
name,password,email
insert into student() values(#{name},#{password},#{email})
參數(shù)處理:
- 單個參數(shù):Mybatis 不會特殊處理
#{參數(shù)名}: 取出參數(shù)值,參數(shù)名任意寫
- 多個參數(shù):Mybatis會做特殊處理,多個參數(shù)會被封裝成一個map
key:param1...paramN,或者參數(shù)的索引也可以(0,1,2,3.....)
value:傳入的參數(shù)值
#{ }就是從map中獲取指定的key的值
命名參數(shù):明確指定封裝參數(shù)時map的key:@param("id")
多個參數(shù)會被封裝成一個map,
key:使用@Param注解指定的值
value:參數(shù)值
#{指定的key}取出對應(yīng)的參數(shù)值
public void updateEmp(@Param("id")Integer id,
@Param("name")String name,
@Param("password")String password,
@Param("email")String email);
update student set name=#{name},password=#{password},email=#{email} where id=#{id}
- POJO參數(shù):如果多個參數(shù)正好是我們業(yè)務(wù)邏輯的數(shù)據(jù)模型,我們就可以直接傳入POJO
#{屬性名}:取出傳入的POJO的屬性值
public void insertEmp(Employee employee);
name,password,email
insert into student() values(#{name},#{password},#{email})
@Test
public void testReturnVal(){
Employee employee = mapper.getEmployeeById(30);
System.out.println(employee);
}
- Map:如果多個參數(shù)不是業(yè)務(wù)模型中的數(shù)據(jù),沒有對應(yīng)的pojo,不經(jīng)常使用,為了方便,我們也可以傳入Map
#{key}:根據(jù) key 取出map中對應(yīng)的值
public void updateName(Map map);
update student set name=#{name} where id=#{id}
@Test
public void testMap(){
Map map = new HashMap<>();
map.put("id", 33);
map.put("name", "劉德華");
mapper.updateName(map);
}
#關(guān)于參數(shù)的問題:
①.使用#{}來傳遞參數(shù)
②.若目標(biāo)方法的參數(shù)類型為對象類型,則調(diào)用其對應(yīng)的getter方法,如getEmail()
③.若目標(biāo)方法的參數(shù)類型為Map類型,則調(diào)用其get(key)
④.若參數(shù)是單個的,或者列表,需要使用@param注解來進(jìn)行標(biāo)記
⑤.注意:若只有一個參數(shù),則可以省略@param注解
若有多個參數(shù),必須要寫@param注解
參數(shù)值的獲取:
#{}:可以獲取map中的值或者pojo對象屬性的值
${}: 可以獲取map中的值獲取pojo對象屬性的值
用例子簡單區(qū)分一下:
select * from tbl_employee where id = ${id} and last_name = #{lastName}
preparing:select * from tbl_employee where id = 2 and last_name = ?
也就是說:對于${} 在日志中可以看到你輸入的值,不安全;
對于#{} 在日志中是?,所以相對安全
具體區(qū)別:
#{}:是以預(yù)編譯的形式,將參數(shù)設(shè)置到sql語句中,相當(dāng)于PreparedStatement;防止sql注入
update student set name=#{name},password=#{password},email=#{email} where id=#{id}
${}:取出的值直接拼裝在sql語句中,會有安全問題
update student set name='${name}',password='${password}',email='${email}' where id='${id}'
大多情況下,我們?nèi)?shù)的值都應(yīng)該去使用#{}
但是原生JDBC不支持占位符的地方我們就可以使用${}進(jìn)行取值
比如獲取表名、分表、排序;按照年份分表拆分
- select * from ${year}_salary where xxx;[表名不支持預(yù)編譯]
- select * from tbl_employee order by ${f_name} ${order} :排序是不支持預(yù)編譯的!
select 元素 :
select元素來定義查詢操作。
Id:唯一標(biāo)識符。
用來引用這條語句,需要和接口的方法名一致
parameterType:參數(shù)類型。
可以不傳,MyBatis會根據(jù)TypeHandler自動推斷
resultType:返回值類型。
別名或者全類名,如果返回的是集合,定義集合中元素的類型。不能和resultMap同時使用
1.返回類型為一個List
public List getEmps();
select * from student
@Test
public void testReturnList(){
List emps = mapper.getEmps();
for (Employee employee : emps) {
System.out.println(employee);
}
}
2.返回記錄為一個Map
只能查詢單條數(shù)據(jù),如果多條的話,多個key 值,找不到
public Map getEmpInfoById(Integer id);
resultType 是 Map 的全類名
select * from student where id = #{id}
key:列名;value:值
@Test
public void testReturnMap(){
Map emp = mapper.getEmpInfoById(30);
Set> entrySet = emp.entrySet();
for (Entry entry : entrySet) {
System.out.println(entry.getKey()+":"+entry.getValue());
}
}
數(shù)據(jù)庫列名與實體類的屬性名不對應(yīng)的情況下有幾種處理方式:
1.sql 語句 用 as 換名
2.下劃線轉(zhuǎn)換成駝峰式命名
在全局配置文件中
3.利用ResultMap:
public Employee getEmpInfoById(Integer id);
//相同的也可以不寫,但因為規(guī)范建議寫
select * from student where id = #{id}
@Test
public void testReturnMap(){
Employee emp = mapper.getEmpInfoById(30);
System.out.println(emp);
}
總結(jié)
以上是生活随笔為你收集整理的mybatis 取查询值_Mybatis --- 映射文件、参数处理、参数值的获取、select元素的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java 如何实现对象克隆_Java对象
- 下一篇: ftp上传文件夹_ftp同步软件哪个好,