Mybatis学习笔记四 查询数据与添加数据

来源:互联网 发布:新加坡高中留学 知乎 编辑:程序博客网 时间:2024/05/20 16:36

添加数据与查询数据

1、 査询数据:在Mybatis框架中,査谪对应的sql语句霈要书写在select标签中。

a、査询时必须指定返回的数据类型

b、如果是带条件的査询语句,则»通过select标签中的parameterType指定条件的类型

2、 添加数据:在Mybatis框架中,添加对应的sql语句需要书写在Insert标签中

a、添加千万不要指定返回的数据类型(增、删、改这三个操作返回类型馱认均为Int类型,代表影响的行数)

b、添加时的参数信息类型*要指定

c、添加数据时需要提交事务(删除、修改也需要提交事务,査询不需要)


这篇文章实在上一篇的基础上的,使用的架构和上一篇中的工程框架相同,使用的数据库和上一次的工程使用的相同。在上一篇的基础上继续了解查询和删除语句。


一、工程目录结构

       


二、代码

Mybatis.xml

      

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><!-- mapper是局部配置文件中的根标签,类似于html文件中的html标签namespace:代表的是唯一的表示符,其值与某一个dao层类的路径保持一致 --><mapper namespace="cn.java.dao.impl.ComputerDaoImpl"><!--在Mybatis中查询语句必须放在select标签中;添加语句必须放在Insert标签中;修改、删除等都必须放在各自对应的标签id:表示的是唯一的表示符  -->  <select id="selectAll" resultType="cn.java.entity.Computer">  SELECT * FROM `book`;  </select>    <!-- 通过id查询 -->  <select id="selectById" parameterType="String" resultType="cn.java.entity.Computer">     select * from book where isbn=#{0}  </select>  <!-- 通过多个条件查询 -->  <select id="selectByConditions" parameterType="Map" resultType="cn.java.entity.Computer">   select * from book where isbn=#{isbn} and price=#{price}  </select>    <!-- 数据添加 -->  <insert id="addComputer" parameterType="Map">   insert into book set isbn=#{isbn},book_name=#{book_name},price=#{price};  </insert></mapper>

定义一个computer类,存放查询的实体

Computer.java

package cn.java.entity;public class Computer {private String isbn;private String book_name;private String price;public String getIsbn() {return isbn;}public void setIsbn(String isbn) {this.isbn = isbn;}public String getBook_name() {return book_name;}public void setBook_name(String book_name) {this.book_name = book_name;}public String getPrice() {return price;}public void setPrice(String price) {this.price = price;}@Overridepublic String toString() {return "Computer [isbn=" + isbn + ", book_name=" + book_name + ", price=" + price + "]";}}

ComputerDaoImpl.java

@before是前置通知,

selectAll()查询数据库中的所有数据

selectById()根据id查询

selectByConditions()多条件查询

addComputer() 添加数据

package cn.java.dao.impl;import static org.hamcrest.CoreMatchers.nullValue;import java.io.IOException;import java.io.InputStream;import java.util.HashMap;import java.util.List;import java.util.Map;import javax.annotation.Resource;import javax.xml.bind.annotation.XmlTransient;import org.apache.ibatis.annotations.One;import org.apache.ibatis.io.Resources;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;import org.junit.Before;import org.junit.Test;import cn.java.entity.Computer;public class ComputerDaoImpl {private static SqlSession session=null;@Beforepublic void init(){try {SqlSessionFactoryBuilder sfb=new SqlSessionFactoryBuilder();InputStream ins=Resources.getResourceAsStream("mybatis.xml");SqlSessionFactory ssf= sfb.build(ins);session=ssf.openSession();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}//查询全部@Testpublic void selectAll() throws IOException{//调用sqlsession类中的方法来执行sql语句List<Computer> lists=session.selectList("cn.java.dao.impl.ComputerDaoImpl.selectAll");for(Computer computer : lists){System.out.println(computer);}}//通过id查询@Testpublic void selectById(){Computer computer=session.selectOne("cn.java.dao.impl.ComputerDaoImpl.selectById", "1001");System.out.println(computer);}//通过多个条件查询@Testpublic void selectByConditions(){Map<String, Object> paramter = new HashMap<String,Object>();paramter.put("isbn", "1002");paramter.put("price", "70");List<Computer> lists=session.selectList("cn.java.dao.impl.ComputerDaoImpl.selectByConditions", paramter);System.out.println(lists);}//添加数据@Testpublic void addComputer(){//将多个参数封装到map集合中去Map<String, Object> parameter=new HashMap<String,Object>();parameter.put("isbn", "1003");parameter.put("book_name", "English");parameter.put("price", "200");int flag=session.insert("cn.java.dao.impl.ComputerDaoImpl.addComputer", parameter);session.commit();if(flag>=1){System.out.println("数据添加成功!");}else{System.out.println("数据添加失败,请重新添加!");}}}
ComputerDaoImpl.xml

注意这个文件名,一定要个上面的类的文件名相同。

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><!-- mapper是局部配置文件中的根标签,类似于html文件中的html标签namespace:代表的是唯一的表示符,其值与某一个dao层类的路径保持一致 --><mapper namespace="cn.java.dao.impl.ComputerDaoImpl"><!--在Mybatis中查询语句必须放在select标签中;添加语句必须放在Insert标签中;修改、删除等都必须放在各自对应的标签id:表示的是唯一的表示符  -->  <select id="selectAll" resultType="cn.java.entity.Computer">  SELECT * FROM `book`;  </select>    <!-- 通过id查询 -->  <select id="selectById" parameterType="String" resultType="cn.java.entity.Computer">     select * from book where isbn=#{0}  </select>  <!-- 通过多个条件查询 -->  <select id="selectByConditions" parameterType="Map" resultType="cn.java.entity.Computer">   select * from book where isbn=#{isbn} and price=#{price}  </select>    <!-- 数据添加 -->  <insert id="addComputer" parameterType="Map">   insert into book set isbn=#{isbn},book_name=#{book_name},price=#{price};  </insert></mapper>





原创粉丝点击