1.简单的pom.xml直接引用就行了
org.mybatis
mybatis
3.4.6
mysql
mysql-connector-java
5.1.44
2.之后是mybatis.xml文件的一个编写(resourse文件夹下)
需要注意的是mapper映射的接口跟以往由xml来实现的接口区别
xml:
注解是不需要xml文件的:
db.properties文件:
user=***
password=***
driverClass=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1/test?characterEncoding=utf-8
initPoolSize=3
maxPoolSize=20
这样mybatis就已经搭建完毕了
直接建立接口之后再mybatis.xml文件里引入mapper就行了
简单的增删改查:
//增加
@Insert("insert into person(pname) values(#{pname})")
public void addPerson(Person person);
//删除
@Delete("delete from person where pid=#{pid}")
public void delPerson(int pid);
//修改
@Update("update person set pname=#{pname} where pid=#{pid}")
public void updatePerson(Person person);
//查询所有
@Select("select * from person")
public List selPersonAll();
//条件查询
@Select("select * from person where pid=#{id}")
public Person selPersonById(int id);
基本上就只需要直接写sql就行了
一对一关系查询:
表关系:
在这就使用person与card类似于人与身份证之间的一对一关系来测试
实体类:
普通实体类就行了,注意如果一对一的话就对象
多对一或者多对多则改为
List集合便可
package com.model;
public class Person {
private int pid;
private String pname;
private Card card;
public Person(int pid, String pname, Card card) {
this.pid = pid;
this.pname = pname;
this.card = card;
}
public Person(int pid, String k, int i) {
}
public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public Card getCard() {
return card;
}
public void setCard(Card card) {
this.card = card;
}
public Person() {
}
}
PersonMapper:
@Select("select * from Person where pid=#{pid}")
//定义结果集
@Results({
//id 表示该属性为主键列 property对应实体类属性 column对应数据库列名
@Result(id = true,property = "pid",column = "pid"),
//还有其他字段可以省略主键列不可省略否则查询为null
//@Result(property="pname",colum="pname")
//one 表示为一的一方 @One(select="根据卡personid从card中查询出card的权限定路径")
@Result(property = "card",column = "pid",one = @One(select ="com.mapper.CardMapper.selCardByPid"))
})
public Person selPersonCardByPid(int pid);
CardMapper:
//根据pid查询出Card
@Select("select * from card where pid=#{pid}")
public Card selCardByPid(int pid);
@Select("select * from card where cid=#{#cid}")
@Results({
@Result(id = true,property = "cid",column = "cid"),
@Result(property = "person",column = "cid",one = @One(select ="com.mapper.PersonMapper.selPersonByCid"))
})
public Card selCardById(int cid);
测试调用OK!
import com.mapper.*;
import com.model.Person;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
public class Test {
private SqlSession sqlSession;
@Before
public void before(){
SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(getClass().getClassLoader().getResourceAsStream("mybatis.xml"));
sqlSession = factory.openSession();
}
@org.junit.Test
public void test(){
PersonMapper mapper=sqlSession.getMapper(PersonMapper.class);
Person person= mapper.selPersonCardByPid(1);
System.out.println("person:"+person.getPname());
System.out.println("card:"+person.getCard().getCname());
}
}
一对多查询:
在这用省份与城市测试,一个省份对应多个城市,一个城市对应一个省份
(自动忽略我的地理常识)
province实体类:
(在这就只展示多的一方,一的一方跟一对一是同理)
package com.model;
public class Card {
private int cid;
private String cname;
private Person person;
public Card() {
}
public Card(int cid, String cname, Person person) {
this.cid = cid;
this.cname = cname;
this.person = person;
}
public int getCid() {
return cid;
}
public void setCid(int cid) {
this.cid = cid;
}
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
}
ProvinceMapper:
package com.mapper;
import com.model.Province;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import java.util.List;
public interface ProvinceMapper {
//查询省份同时查询出城市
@Select("select * from province where pid=#{pid}")
@Results({
@Result(id = true,property = "pid",column = "pid"),
//many表示多 eg:select * from city where pid=1(查询城市表中省份id为1的)
@Result(property = "citys",column = "pid",many = @Many(select ="com.mapper.CityMapper.selCityByPid"))
})
public Province selProvinceById(int pid);
}
CityMapper:
package com.mapper;
import com.model.City;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import java.util.List;
public interface CityMapper {
//查询城市同时查询出省份
@Select("select * from city where cid=#{cid}")
@Results({
@Result(id = true,property = "cid",column = "cid"),
@Result(property = "province",column = "pid",one = @One(select ="com.mapper.ProvinceMapper.selProvinceById"))
})
public City selCityById(int cid);
//根据省份id查询city
@Select("select * from city where pid=#{pid}")
public List selCityByPid(int pid);
}
测试OK!
ProvinceMapper mapper=sqlSession.getMapper(ProvinceMapper.class);
Province province=mapper.selProvinceById(1);
System.out.println("省份:"+province.getPname());
System.out.println("城市:");
for (City city : province.getCitys()) {
System.out.println(city.getSname());
}
多对多:
(多对多则需要一张关系表也就是第三方表,在这使用学生与选修课程做多对多关系)
student:
Student实体类(course 反之):
第三方表只做查询无需实体类
package com.model;
import java.util.List;
public class Student {
private Integer sid;
private String sname;
private List courses;
public Student(Integer sid, String sname, List courses) {
this.sid = sid;
this.sname = sname;
this.courses = courses;
}
public Student() {
}
public Integer getSid() {
return sid;
}
public void setSid(Integer sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public List getCourses() {
return courses;
}
public void setCourses(List courses) {
this.courses = courses;
}
}
StudentMapper:
package com.mapper;
import com.model.Student;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import java.util.List;
public interface StudentMapper {
//根据学生id查询出学生并与其选修课程
@Select("select * from student where sid=#{sid}")
@Results({
@Result(id = true,property = "sid",column = "sid"),
@Result(property = "courses",column = "sid",many = @Many(select = "com.mapper.CourseMapper.selCouseBySid"))
})
public Student selStudentById(int sid);
//跟据课程id关联学生表查出选修课程的学生
@Select("select * from student a,stu_cou b where a.sid=b.sid and b.cid=#{#cid}")
public List selStudentByCid(int cid);
}
CourseMapper:
package com.mapper;
import com.model.Student;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import java.util.List;
public interface StudentMapper {
//根据学生id查询出学生并与其选修课程
@Select("select * from student where sid=#{sid}")
@Results({
@Result(id = true,property = "sid",column = "sid"),
@Result(property = "courses",column = "sid",many = @Many(select = "com.mapper.CourseMapper.selCouseBySid"))
})
public Student selStudentById(int sid);
//跟据课程id关联学生表查出选修课程的学生
@Select("select * from student a,stu_cou b where a.sid=b.sid and b.cid=#{#cid}")
public List selStudentByCid(int cid);
}
测试OK!
StudentMapper mapper=sqlSession.getMapper(StudentMapper.class);
Student student= mapper.selStudentById(1);
System.out.println("学生姓名:"+student.getSname());
System.out.println("学生选修课程:");
for (Course cours : student.getCourses()) {
System.out.println(cours.getCname());
}
注解方式写MyBatis简单的就差不多了!