mybatis 使用接口增删改查和两表一对一关联查询
导包
总配置文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <properties resource="db.properties"/> <typeAliases> <package name="com.model"/> </typeAliases> <environments default="test"> <environment id="test"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${driver}"/> <property name="url" value="${jdbcUrl}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </dataSource> </environment> </environments> <mappers> <package name="com.dao"/><!-- 自动装配包里的接口 --> </mappers> </configuration>
driver=oracle.jdbc.OracleDriver jdbcUrl=jdbc:oracle:thin:@localhost:1521:orcl username=test1116 password=123456
数据库
两个实体类
package com.model; import java.util.Date; public class Student { private Integer sno; private String sname; private String ssex; private Integer sclass; public Student() { super(); } public Student(Integer sno, String sname, String ssex, Integer sclass) { super(); this.sno = sno; this.sname = sname; this.ssex = ssex; this.sclass = sclass; } public Integer getSno() { return sno; } public void setSno(Integer sno) { this.sno = sno; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public String getSsex() { return ssex; } public void setSsex(String ssex) { this.ssex = ssex; } @Override public String toString() { return "Student [sno=" + sno + ", sname=" + sname + ", ssex=" + ssex + ", sclass=" + sclass + "]"; } public Integer getSclass() { return sclass; } public void setSclass(Integer sclass) { this.sclass = sclass; } }
package com.model; import java.util.Date; public class StudentInfo { private Integer id; private Student student; private String saddress; private Date sbirthday; public StudentInfo() { super(); } public StudentInfo(Integer id, Student student, String saddress, Date sbirthday) { super(); this.id = id; this.student = student; this.saddress = saddress; this.sbirthday = sbirthday; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public Student getStudent() { return student; } public void setStudent(Student student) { this.student = student; } public String getSaddress() { return saddress; } public void setSaddress(String saddress) { this.saddress = saddress; } public Date getSbirthday() { return sbirthday; } public void setSbirthday(Date sbirthday) { this.sbirthday = sbirthday; } @Override public String toString() { return "StudentInfo [id=" + id + ", student=" + student + ", saddress=" + saddress + ", sbirthday=" + sbirthday + "]"; } }
两个接口
package com.dao; import java.util.List; import java.util.Map; import com.model.Student; public interface StudentMapper { /** * 添加 */ public Integer add(Student student); /** * 删除 */ public Integer delete(Integer son); /** * 修改 */ public Integer update(Student student); /** * 根据主键查查询 */ public Student select(Integer sno); /** * 全表查询 */ public List<Student> selectall(); /** * 多条件查询 */ public List<Student> selectduo(Map<String, Object> map); }
package com.dao; import java.util.List; import com.model.StudentInfo; public interface StudentInfoMapper { /** * 一对一关联查询 * @return */ public List<StudentInfo> selectall(); }
对应的两个配置文件,注意接口名要跟实配置文件名字一样,相当与实现了接口
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.dao.StudentMapper"> <!-- 添加 --> <insert id="add" parameterType="student"> insert into student values(#{sno},#{sname},#{ssex},#{sclass}) </insert> <!-- 删除 --> <delete id="delete" parameterType="Integer"> delete student s where s.sno=#{sno} </delete> <!-- 修改 --> <update id="update" parameterType="student"> update student s set s.sname=#{sname},s.ssex=#{ssex},s.sclass=#{sclass} where s.sno=#{sno} </update> <!-- 根据主键查 --> <select id="select" parameterType="Integer" resultType="student"> select * from student s where s.sno=#{sno} </select> <!-- 查全部 --> <select id="selectall" resultType="student" > select * from student </select> <!-- 多条件查询 --> <select id="selectduo" parameterType="Map" resultType="student" > select * from student s where s.sno=#{sno} and s.sname like #{sname} </select> </mapper>
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.dao.StudentInfoMapper"> <!-- 一对一关联查询 --> <resultMap type="studentInfo" id="silist"> <result property="id" column="id"/> <result property="student.sno" column="sno"/> <result property="student.sname" column="sname"/> <result property="student.ssex" column="ssex"/> <result property="student.sclass" column="sclass"/> <result property="saddress" column="saddress"/> <result property="sbirthday" column="sbirthday"/> </resultMap> <!-- 一对一另一种方法 --> <resultMap type="StudentInfo" id="silist1"> <association property="student" column="sno" select="com.dao.StudentMapper.select"/> </resultMap> <select id="selectall" resultMap="silist1"> select * from studentinfo si left join student s on si.sno=s.sno </select> </mapper>
sqlsession工具类
package com.util; import java.io.IOException; import java.io.InputStream; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; public class SqlSessionUtil { private static SqlSessionFactory ssf; private static SqlSession ss; public static SqlSessionFactory getSqlSessionFactory(){ InputStream in=null; try { in=Resources.getResourceAsStream("mybatis-cfg.xml"); ssf=new SqlSessionFactoryBuilder().build(in); in.close(); } catch (IOException e) { e.printStackTrace(); } return ssf; } public static SqlSession getSqlSession(){ ss=getSqlSessionFactory().openSession(); return ss; } public static void main(String[] args) { System.out.println(getSqlSession()); } }
第一个junit测试
package com.util; import static org.junit.Assert.*; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.ibatis.session.SqlSession; import org.junit.After; import org.junit.Before; import org.junit.Test; import com.dao.StudentMapper; import com.model.Student; public class Jtest { private SqlSession ss; private StudentMapper sm; @Before public void setUp() throws Exception { ss=SqlSessionUtil.getSqlSession(); sm=ss.getMapper(StudentMapper.class); } @After public void tearDown() throws Exception { ss.commit(); ss.close(); } public void test() { Student st=new Student(6, "蒙蒙", "男", 95031); int m=sm.add(st); System.out.println(m); } public void delete() { int m=sm.delete(6); System.out.println(m); } public void update() { Student st=new Student(666, "张三", "男", 95033); int m=sm.update(st); System.out.println(m); } public void select() { Student st=sm.select(3); System.out.println(st); } public void selectall() { List<Student> st=sm.selectall(); for(Student tt:st){ System.out.println(tt); } } @Test public void selectduo() { Map<String, Object> map=new HashMap<String, Object>(); map.put("sno", 666); map.put("sname", "%张%"); List<Student> st=sm.selectduo(map); for(Student tt:st){ System.out.println(tt); } } }
第二个junit测试
package com.util; import static org.junit.Assert.*; import java.util.List; import org.apache.ibatis.session.SqlSession; import org.junit.After; import org.junit.Before; import org.junit.Test; import com.dao.StudentInfoMapper; import com.dao.StudentMapper; import com.model.StudentInfo; public class JJtest { private SqlSession ss; private StudentInfoMapper sim; @Before public void setUp() throws Exception { ss=SqlSessionUtil.getSqlSession(); sim=ss.getMapper(StudentInfoMapper.class); } @After public void tearDown() throws Exception { ss.commit(); ss.close(); } @Test public void test() { List<StudentInfo> list=sim.selectall(); for(StudentInfo a:list){ System.out.println(a); } } }
多条件查询结果,其它的就不发了