工程环境: JDK1.8 + Mybatis3 + JUnit + Mysql
1. 在数据库中建表
学生表:
create table student(id int auto_increment primary key,name varchar(10) unique, age int);
2. 运用Mybatis Generator反向生成model,mapper
请参考Mybatis入门学习篇(一)之Mybatis Generator使用
Model-Student:
package com.zjh.model;
public class Student {
private Integer id;
private String name;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name == null ? null : name.trim();
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
Mapper-StudentMapper:
package com.zjh.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectKey;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.mapping.StatementType;
import org.apache.ibatis.type.JdbcType;
import com.zjh.model.Student;
public interface StudentMappper {
//基于注解的增删改查
@Update("update student set name = #{student.name},age = #{student.age} where id = #{student.id}")
@Result(jdbcType = JdbcType.INTEGER)
public int update(@Param("student") Student student);
@Select("select * from student where name like '%'||#{name}||'%'")
@Result(javaType = Student.class)
public List findByName(@Param("name") String name);
@Select("select * from student where age = #{age}")
@Result(javaType = Student.class)
public List findByAge(@Param("age") Integer age);
@Insert("insert into student(id,name,age) values(null,#{student.name},#{student.age})")
@Result(jdbcType = JdbcType.INTEGER)
@SelectKey(keyProperty = "student.id", keyColumn = "id", statement = "select @@identity as id", statementType = StatementType.STATEMENT, resultType = Integer.class, before = false)
public int insert(@Param("student") Student student);
@Delete("delete from student where id = #{student.id}")
@Result(jdbcType = JdbcType.INTEGER)
public int delete(@Param("student") Student student);
//以下由工具自动生成,已自动生成配置信息
int deleteByPrimaryKey(Integer id);
int insertSelective(Student record);
Student selectByPrimaryKey(Integer id);
int updateByPrimaryKeySelective(Student record);
int updateByPrimaryKey(Student record);
}
Mapper-StudentMapper.xml
id, name, age
delete from student
where id = #{id,jdbcType=INTEGER}
insert into student (id, name, age
)
values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR},
#{age,jdbcType=INTEGER}
)
insert into student
id,
name,
age,
#{id,jdbcType=INTEGER},
#{name,jdbcType=VARCHAR},
#{age,jdbcType=INTEGER},
update student
name = #{name,jdbcType=VARCHAR},
age = #{age,jdbcType=INTEGER},
where id = #{id,jdbcType=INTEGER}
update student
set name = #{name,jdbcType=VARCHAR},
age = #{age,jdbcType=INTEGER}
where id = #{id,jdbcType=INTEGER}
3. 配置mybatis需要的信息
在根目录下新建xml文件,取名为mybatis-config.xml
4. 测试
这里测试使用的是Junit
首先是获得Session
public SqlSession getSession() {
//通过Resources读取mybatis配置文件
try (Reader resource = Resources
.getResourceAsReader("mybatis-config.xml")) {
//根据配置文件建立SessioFactory
SqlSessionFactory factory = new SqlSessionFactoryBuilder()
.build(resource);
//将StudentMapper在SessioFactory中注册
factory.getConfiguration().addMapper(StudentMappper.class);
//获取session
return factory.openSession();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
下面依次做插入,删除,更新,查询的测试
@org.junit.Test
public void insert() throws IOException {
SqlSession session = getSession();
Student student = new Student();
student.setAge(21);
student.setName("zjh63248");
StudentMappper dao = session.getMapper(StudentMappper.class);
System.out.println(dao.insert(student));
session.commit();
session.close();
System.out.println(student.getId());
}
@org.junit.Test
public void select() {
SqlSession session = getSession();
StudentMappper dao = session.getMapper(StudentMappper.class);
List list = dao.findByName("zjh");
for (Student s : list) {
System.out.println(s.getName() + "的年龄是" + s.getAge());
}
}
@org.junit.Test
public void delete() {
SqlSession session = getSession();
StudentMappper dao = session.getMapper(StudentMappper.class);
Student student = new Student();
student.setAge(21);
student.setName("zjh63248");
student.setId(26);
System.out.println(dao.delete(student));
session.commit();
session.close();
}
@org.junit.Test
public void update() {
SqlSession session = getSession();
StudentMappper dao = session.getMapper(StudentMappper.class);
Student student = new Student();
student.setAge(21);
student.setName("zjh");
student.setId(27);
System.out.println(dao.update(student));
session.commit();
session.close();
}
到此结束,期间遇到的问题,会在下一章节做记录.
源码下载:http://download.csdn.net/detail/u013769320/8320717