您好,登錄后才能下訂單哦!
這篇文章主要介紹“mybatis的映射文件mapper.xml怎么寫”,在日常操作中,相信很多人在mybatis的映射文件mapper.xml怎么寫問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”mybatis的映射文件mapper.xml怎么寫”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
在學習mybatis的時候我們通常會在映射文件這樣寫:
<?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="com.qbd.mapper.StudentMappers"> <select id="findbyid" parameterType="Integer" resultMap="StudentResult"> select *from student where id=#{id} </select> <select id="findbygradeid" parameterType="Integer" resultMap="StudentResult"> select *from student where gid=#{gid} </select> <resultMap type="Student" id="StudentResult"> <id property="id" column="id"/> <result property="name" column="name"/> <result property="age" column="age"/> <association property="address" column="addid" select="com.qbd.mapper.AddressMappers.findbyid"> </association> <association property="grade" column="gid" select="com.qbd.mapper.GradeMappers.findbyid"> </association> </resultMap> </mapper>
然后再寫dao的實現在寫一個類,來實現dao的接口。但是這樣在實現中需要這樣寫:
package com.qbd.service; import org.apache.ibatis.session.SqlSession; import org.apache.log4j.Logger; import com.qbd.mapper.StudentMappers; import com.qbd.model.Student; import com.qbd.util.SqlSessionFactoryUtil; public class StudentService { private static Logger logge=Logger.getLogger(StudentService.class); public static void main(String[] args) { SqlSession sqlSession=SqlSessionFactoryUtil.getSqlSession(); StudentMappers studentMappers=(StudentMappers)sqlSession.getMapper(StudentMappers.class); Student student=new Student(); student.setName("22"); student.setAge(2); int s=studentMappers.add(student); sqlSession.commit(); if(s>0){ logge.info("success"); System.out.println("success"); } } }
來讀取配置文件
另一種方法就是:
直接在mapper.xml中的這一部分寫成dao<mapper namespace="com.qbd.mapper.StudentMappers">如下
<?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="com.qbd.ssm.dao.UserDao"> <!-- 定義緩存 一般是一級緩存,如果用同一個sqlsession 那么相同查詢直接會從緩存中查找 <cache size="1024" flushInterval="60000" eviction="LRU" readOnly="false"></cache> --> <!-- 查找所有 --> <select id="find" parameterType="Map" resultMap="StudentResult"> select * from user <where> <if test="uname!=null and uname!='' "> and uname like #{uname} </if> </where> <if test="start!=null and size!=null"> limit #{start},#{size} </if> </select> <select id="getTotal" parameterType="Map" resultType="Long"> select count(*) from user <where> <if test="uname!=null and uname!='' "> and uname like #{uname} </if> </where> </select> <!-- 按照用戶名和密碼查找 --> <select id="getUser" resultMap="StudentResult" parameterType="Map"> select *from user where uname=#{uname} and upassword=#{upassword} </select> <!-- 刪除 --> <delete id="delete" parameterType="Map"> delete from user where uid=#{uid} </delete> <!-- 修改 --> <update id="update" parameterType="User"> update user <set> <if test="uname!=null"> uname=#{uname}, </if> <if test="upassword!=null"> upassword=#{upassword}, </if> <if test="upower!=null"> upower=#{upower}, </if> </set> where uid=#{uid} </update> <!-- 增加 --> <insert id="add" parameterType="User"> insert into user values(null,#{uname},#{upassword},#{upower}) </insert> <resultMap type="User" id="StudentResult"> <id property="uid" column="uid"/> <result property="uname" column="uname"/> <result property="upassword" column="upassword"/> </resultMap> </mapper>
那么就不用寫dao的實現在service中就能掉用 mybatis默認會把mapper.xml映射為dao的實現
那么下面dao就能這樣寫:
package com.qbd.ssm.dao; import java.util.List; import java.util.Map; import org.apache.ibatis.annotations.Delete; import com.qbd.ssm.model.User; public interface UserDao { public List<User> getAll(); public User getUser(User user); public int delete(User user); public int update(User user); public int add(User user); public List<User> find(Map<String,Object> map); public Long getTotal(Map<String,Object> map); }
service這樣寫
package com.qbd.ssm.service; import java.util.List; import java.util.Map; import com.qbd.ssm.model.User; public interface UserService { public List<User> getAll(); public User getUser(User user); public int delete(User user); public int update(User user); public int add(User user); public List<User> find(Map<String,Object> map); public Long getTotal(Map<String,Object> map); }
service的實現:
package com.qbd.ssm.serviceimpl; import java.util.List; import java.util.Map; import javax.annotation.Resource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.qbd.ssm.dao.UserDao; import com.qbd.ssm.model.User; import com.qbd.ssm.service.UserService; @Service("userService") public class UserServiceImpl implements UserService { private UserDao userDao; public UserDao getUserDao() { return userDao; } @Resource public void setUserDao(UserDao userDao) { this.userDao = userDao; } public List<User> getAll() { // TODO Auto-generated method stub return userDao.getAll(); } public User getUser(User user) { // TODO Auto-generated method stub return userDao.getUser(user); } public int delete(User user) { // TODO Auto-generated method stub return userDao.delete(user); } public int update(User user) { // TODO Auto-generated method stub return userDao.update(user); } public int add(User user) { // TODO Auto-generated method stub return userDao.add(user); } public List<User> find(Map<String, Object> map) { // TODO Auto-generated method stub return userDao.find(map); } public Long getTotal(Map<String, Object> map) { // TODO Auto-generated method stub return userDao.getTotal(map); } }
在這里面userDao不能寫錯,spring會按照name進行注入
到此,關于“mybatis的映射文件mapper.xml怎么寫”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。