insert 方法:
@Test
public void testInsert() throws IOException {
Student student = new Student();
student.setId(1006);
student.setName("林浩");
student.setEmail("[email protected]");
student.setAge(26);
int nums = studentDao.insertStudent(student);
System.out.println("使用 Dao 添加数据:"+nums);
}
update 方法
@Test
public void testUpdate() throws IOException {
Student student = new Student();
student.setId(1006);
student.setAge(28);
int nums = studentDao.updateStudent(student);
System.out.println("使用 Dao 修改数据:"+nums);
}
delete 方法
@Test
public void testDelete() throws IOException {
int nums = studentDao.deleteStudent(1006);
System.out.println("使用 Dao 修改数据:"+nums);
}
sqlSession.getMapper (dao接口.class)
获取这个dao接口的对象public
Student
selectStudentById
(Integer Id)
mybatis把java的基本数据类型String都叫简单类型
在mapper文件获取简单类型的一一个参数的值,使用#{任意字符}
Student student = studentDao.selectById(1005);
当 Dao 接口方法多个参数,需要通过名称使用参数。在方法形参前面加入@Param(“自定义参数名”),mapper
文件使用#{自定义参数名}
mapper 文件
<select id="selectById" resultType="work01_mybatis_dao.content.domain.Student">
select id,name,email,age from student where id=#{studentId}
</select>
转为 MyBatis 的执行是:
String sql=” select id,name,email,age from student where id=?”;
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1,1005);
resultType: 执行 sql 得到 ResultSet 转换的类型,使用类型的完全限定名或别名。 注意如果返回的是集合,那应该设置为集合包含的类型,而不是集合本身。resultType 和 resultMap,不能同时使用
接口方法:
int countStudent();
mapper 文件:
<select id="countStudent" resultType="int">
select count(*) from student
select>
测试方法:
@Test
public void testRetunInt(){
int count = studentDao.countStudent();
System.out.println("学生总人数:"+ count);
}
接口方法:
Student selectById(int id);
mapper 文件:
<select id="selectById" resultType="com.bjpowernode.domain.Student">
select id,name,email,age from student where id=#{studentId}
select>
框架的处理: 使用构造方法创建对象。调用 setXXX 给属性赋值
Student student = new Student();
sql 语句列 java 对象方法
id setId( rs.getInt(“id”) ) 调用列名对应的 set 方法
id 列 --- setId()
name 列 --- setName()
name setName( rs.getString(“name”) )
email setEmail( rs.getString(“email”) )
age setAge( rs.getInt(“age”) )
注意:Dao 接口方法返回是集合类型,需要指定集合中的类型,不是集合本身
Map
推荐使用 Map
注意:Map 作为接口返回值,sql 语句的查询结果最多只能有一条记录。大于一条记录是错误
接口方法:
Map<Object,Object> selectReturnMap(int id);
mapper 文件:
<select id="selectReturnMap" resultType="java.util.HashMap">
select name,email from student where id = #{studentId}
select>
测试方法:
@Test
public void testReturnMap(){
Map<Object,Object> retMap = studentDao.selectReturnMap(1002);
System.out.println("查询结果是 Map:"+retMap);
}
resultMap 可以自定义 sql 的结果和 java 对象属性的映射关系
更灵活的把列值赋值给指定属性 常用在列名和 java
对象属性名不一样的情况
使用方式:
List<Student> selectUseResultMap(QueryParam param);
mapper 文件:
<resultMap id="studentMap" type="com.bjpowernode.domain.Student">
<id column="id" property="id" />
<result column="name" property="name"/>
<result column="email" property="email" />
<result column="age" property="age" />
resultMap>
<select id="selectUseResultMap" resultMap="studentMap">
select id,name,email,age from student where name=#{queryName} or age=#{queryAge}
select>
测试方法:
@Test
public void testSelectUseResultMap(){
QueryParam param = new QueryParam();
param.setQueryName("李力");
param.setQueryAge(20);
List<Student> stuList = studentDao.selectUseResultMap(param);
stuList.forEach( stu -> System.out.println(stu));
}
(1) 使用列别名和
步骤:
/**
* Description: 实体类
* Company: http://www.bjpowernode.com
*/
public class PrimaryStudent {
private Integer stuId;
private String stuName;
private Integer stuAge;
// set , get 方法
}
<select id="selectUseFieldAlias"
resultType="com.bjpowernode.domain.PrimaryStudent">
select id as stuId, name as stuName,age as stuAge
from student where name=#{queryName} or age=#{queryAge}
select>
@Test
public void testSelectUseFieldAlias(){
QueryParam param = new QueryParam();
param.setQueryName("李力");
param.setQueryAge(20);
List<PrimaryStudent> stuList;
stuList = studentDao.selectUseFieldAlias(param);
stuList.forEach( stu -> System.out.println(stu));
}
(2) 使用
步骤:
List<PrimaryStudent> selectUseDiffResultMap(QueryParam param);
<resultMap id="primaryStudentMap"
type="com.bjpowernode.domain.PrimaryStudent">
<id column="id" property="stuId" />
<result column="name" property="stuName"/>
<result column="age" property="stuAge" />
resultMap>
<select id="selectUseDiffResultMap" resultMap="primaryStudentMap">
select id,name,email,age from student
where name=#{queryName} or age=#{queryAge}
select>
@Test
public void testSelectUseDiffResultMap(){
QueryParam param = new QueryParam();
param.setQueryName("李力");
param.setQueryAge(20);
List<PrimaryStudent> stuList;
stuList = studentDao.selectUseDiffResultMap(param);
stuList.forEach( stu -> System.out.println(stu));
}
模糊查询的实现有两种方式
例 1: java 代码中提供要查询的 “%力%”
接口方法:
List<Student> selectLikeFirst(String name);
mapper 文件:
<select id="selectLikeFirst" resultType="com.bjpowernode.domain.Student">
select id,name,email,age from student
where name like #{studentName}
select>
测试方法:
@Test
public void testSelectLikeOne(){
String name="%力%";
List<Student> stuList = studentDao.selectLikeFirst(name);
stuList.forEach( stu -> System.out.println(stu));
}
例 2:mapper 文件中使用 like name “%” #{xxx} "%"
接口方法:
List<Student> selectLikeSecond(String name);
mapper 文件:
<select id="selectLikeSecond" resultType="com.bjpowernode.domain.Student">
select id,name,email,age from student
where name like "%" #{studentName} "%"
select>
测试方法:
@Test
public void testSelectLikeSecond(){
String name="力";
List<Student> stuList = studentDao.selectLikeSecond(name);
stuList.forEach( stu -> System.out.println(stu));
}