Mybatis 接口方式对数据的增删改查 一对一关联查询
数据库中有两个表 student 和studentInfo
student表中的字段和数据
studentInfo表中的字段
ok数据库说完了,开始建立一个项目,对数据库中的数据进行操作吧
新建java项目,将mybatis的jar包和oracle数据库的访问包导入
建立几个需要用的包 entities, dao,util , test建立映射文件(.xml)和db.properties文件
db.properties:
mybatis-config.xml:
<?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"> <!-- mybatis配置的根标签 --> <configuration> <properties resource="db.properties"></properties> <!--自动扫描实体类 --> <typeAliases> <package name="com.maya.entities" /> </typeAliases> <!--default等于那个就执行那个 --> <environments default="test"> <environment id="test"> <transactionManager type="JDBC"></transactionManager> <dataSource type="POOLED"> <property name="driver" value="${driver}" /> <property name="url" value="${jdbc}" /> <property name="username" value="${user}" /> <property name="password" value="${password}" /> </dataSource> </environment> </environments> <!-- 映射 --> <mappers> <package name="com.maya.dao" /> </mappers> </configuration>
这样,mybatis的配置文件配置完成,接下来写util文件(SqlSessionFactory)
package com.maya.util; 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 sessionFactory; private static SqlSession session; public static SqlSession getSqlSession(){ InputStream input=null; try { input=Resources.getResourceAsStream("mybatis-config.xml"); sessionFactory=new SqlSessionFactoryBuilder().build(input); session=sessionFactory.openSession(); input.close(); } catch (Exception e) { e.printStackTrace(); } return session; } }
接下来写实体类:需要注意的是,如果我们需要查student,需要把studentInfo表中的数据一起查出来的话,需要写出对应的关系;(篇幅原因,get/set和构造函数就不贴进来了)
Student
package com.maya.entities; public class Student { private StudentInfo studentInfo; //studentInfo的实体类 private String sname; private String ssex; private Integer sclass; private Integer mark; }
StudentInfo
package com.maya.entities; import java.sql.Date; public class StudentInfo { private Integer id; private Integer sno; private String saddress; private Date sbirthday; }
实体类建立完成,接下来写接口,和xml文件
package com.maya.dao; import java.util.List; import com.maya.entities.StudentInfo; public interface StudentInfoMapper { //查询所有 public List<StudentInfo> select(); //根据sno查询 public StudentInfo selectbysno(); }
<?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.maya.dao.StudentInfoMapper"> <!-- 查询整个studentInfo表 --> <select id="select" resultType="studentInfo"> select * from studentinfo </select> </mapper>
建立一个junit测试用例
package com.maya.test; 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.maya.dao.StudentInfoMapper; import com.maya.entities.StudentInfo; import com.maya.util.SqlSessionUtil; public class TestJunit { private SqlSession session; private StudentInfoMapper sim; @Before public void setUp() throws Exception { //获取sqlsession 和StudentInfoMapper session=SqlSessionUtil.getSqlSession(); sim=session.getMapper(StudentInfoMapper.class); } @After public void tearDown() throws Exception { session.commit(); //提交 session.close(); //关闭 } @Test public void test() { List<StudentInfo> list=sim.select(); for(StudentInfo info:list){ System.out.println(info); } } }
执行结果如下
说明我们前面配置的没有问题
建立Student接口和配置文件
package com.maya.dao; import java.util.List; import java.util.Map; import com.maya.entities.Student; public interface StudentMapper { //查询出所有 public List<Student> select(); //多条件查询 public List<Student> select(Map<String,Object> map); }
<?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.maya.dao.StudentMapper"> <!--接口的限定名 --> <resultMap type="student" id="list"> <association property="studentInfo" column="sno" select="com.maya.dao.StudentInfoMapper.selectbysno"/> </resultMap> <select id="select" resultMap="list"> <!--返回的值不是单一的表的值时用Map --> select * from student s join studentinfo si on s.sno=si.sno </select> </mapper>
junit测试:
package com.maya.test; 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.maya.dao.StudentInfoMapper; import com.maya.dao.StudentMapper; import com.maya.entities.Student; import com.maya.entities.StudentInfo; import com.maya.util.SqlSessionUtil; public class TestJunit { private SqlSession session; private StudentInfoMapper sim; private StudentMapper sdm; @Before public void setUp() throws Exception { //获取sqlsession 和StudentInfoMapper session=SqlSessionUtil.getSqlSession(); sim=session.getMapper(StudentInfoMapper.class); sdm=session.getMapper(StudentMapper.class); } @After public void tearDown() throws Exception { session.commit(); //提交 session.close(); //关闭 } @Test public void test() { List<Student> list=sdm.select(); for(Student stu:list){ System.out.println(stu); } // List<StudentInfo> list=sim.select(); // for(StudentInfo info:list){ // System.out.println(info); // } // } }
结果如下
我们不是制造者,我们是代码搬运工