public interface EmployeeMapper { /* * 增刪改查方法 * */ public Employee getEmployeeById(Integer id); public void insertEmp(Employee employee); }
<select id="getEmployeeById" resultType="employee" databaseId="mysql"> select * from student where id = #{id} </select> <insert id="insertEmp" parameterType="com.neuedu.entity.Employee" useGeneratedKeys="true" keyProperty="id"> insert into student(name,password,email) values(#{name},#{password},#{email}) </insert>
編寫測試單元:java
private EmployeeMapper mapper = null; private SqlSession session = null; @Before public void testBefore(){ //1.獲取sqlSessionFactory對象 SqlSessionFactory sqlSessionFactory = getSqlSessionFactory(); //2.利用sqlSessionFactory建立一個session對象,表示和數據庫的一次會話 session = sqlSessionFactory.openSession(); //3.用session對象獲取mapper接口的代理對象 //由於sql映射文件給相應的接口建立了一個代理對象,因此mapper接口類不須要實現類 mapper = session.getMapper(EmployeeMapper.class); } @Test public void testSelect(){ mapper = session.getMapper(EmployeeMapper.class); //4.經過mapper接口的代理對象就能夠對數據庫進行增刪改查操做 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(){ //增刪改須要提交事務 session.commit(); session.close(); } //@Before、@After自動在@Test以前和以後運行 //查詢不須要提交事務,增刪改都須要提交事務
2.獲取自增主鍵值【當向數據庫中插入一條數據的時候,默認是拿不到主鍵的值的, 須要設置以下兩個屬性才能夠拿到主鍵值!】mysql
<!--設置userGeneratedKeys屬性值爲true:使用自動增加的主鍵。使用keyProperty設置把主鍵值設置給哪個屬性--> <insert id="addEmp" parameterType="com.neuedu.mybatis.bean.Employee" useGeneratedKeys="true" keyProperty="id" databaseId="mysql"> insert into tbl_employee(last_name,email,gender) values(#{lastName},#{gender},#{email}) </insert>
<sql id="npe"> name,password,email </sql> <insert id="insertEmp" parameterType="com.neuedu.entity.Employee" useGeneratedKeys="true" keyProperty="id"> insert into student(<include refid="npe"></include>) values(#{name},#{password},#{email}) </insert>
public void updateEmp(@Param("id")Integer id, @Param("name")String name, @Param("password")String password, @Param("email")String email);
<update id="updateEmp"> update student set name=#{name},password=#{password},email=#{email} where id=#{id} </update>
- POJO參數:若是多個參數正好是咱們業務邏輯的數據模型,咱們就能夠直接傳入POJOsql
#{屬性名}:取出傳入的POJO的屬性值數據庫
public void insertEmp(Employee employee);
<sql id="npe"> name,password,email </sql> <insert id="insertEmp" parameterType="com.neuedu.entity.Employee" useGeneratedKeys="true" keyProperty="id"> insert into student(<include refid="npe"></include>) values(#{name},#{password},#{email}) </insert>
@Test public void testReturnVal(){ Employee employee = mapper.getEmployeeById(30); System.out.println(employee); }
- Map:若是多個參數不是業務模型中的數據,沒有對應的pojo,不常常使用,爲了方便,咱們也能夠傳入Map緩存
#{key}:根據 key 取出map中對應的值安全
public void updateName(Map<String, Object> map);
<update id="updateName"> update student set name=#{name} where id=#{id} </update>
@Test public void testMap(){ Map<String, Object> map = new HashMap<>(); map.put("id", 33); map.put("name", "劉德華"); mapper.updateName(map); }
<update id="updateEmp"> update student set name=#{name},password=#{password},email=#{email} where id=#{id} </update>
${}:取出的值直接拼裝在sql語句中,會有安全問題session
<update id="updateEmp"> update student set name='${name}',password='${password}',email='${email}' where id='${id}' </update>
public List<Employee> getEmps();
<select id="getEmps" resultType="com.neuedu.entity.Employee"> select * from student </select>
@Test public void testReturnList(){ List<Employee> emps = mapper.getEmps(); for (Employee employee : emps) { System.out.println(employee); } }
2.返回記錄爲一個Mapmybatis
只能查詢單條數據,若是多條的話,多個key 值,找不到app
public Map<String, Object> getEmpInfoById(Integer id);
resultType 是 Map 的全類名函數
<select id="getEmpInfoById" ="java.util.Map"> select * from student where id = #{id} </select>
key:列名;value:值
@Test public void testReturnMap(){ Map<String, Object> emp = mapper.getEmpInfoById(30); Set<Entry<String,Object>> entrySet = emp.entrySet(); for (Entry<String, Object> entry : entrySet) { System.out.println(entry.getKey()+":"+entry.getValue()); } }
<settings> <!-- setting標籤負責每個屬性的設置 --> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings>
3.利用ResultMap:
public Employee getEmpInfoById(Integer id);
<resultMap type="com.neuedu.entity.Employee" id="getEmployByIdMap"> <!-- 主鍵映射能夠用 id 字段,mybatis在底層會作優化,主鍵有索引,加快查詢速度 --> <id column="id" property="id"/> <!-- 普通列的映射使用result --> <result column="name" property="name"/> <result column="password" property="password"/>//相同的也能夠不寫,但由於規範建議寫 <result column="email" property="email"/> </resultMap> <select id="getEmpInfoById" resultMap="getEmployByIdMap"> select * from student where id = #{id} </select>
@Test public void testReturnMap(){ Employee emp = mapper.getEmpInfoById(30); System.out.println(emp); }