一、获取session,session直接调用mapper配置文件里写的方法。
二、获取session,session再通过生成mapper接口实现类来查询。这里需要注意的就是,如mapper/StudentMapper.java接口需要同xmlmaper文件的namespace一致。
1 public class TestXmlBuild1 { 2 public static SqlSessionFactory sqlMapper = null; 3 4 @Before 5 public void setUpClass() throws IOException { 6 String resource = "config/Configuration1.xml"; 7 Reader reader = Resources.getResourceAsReader(resource); 8 sqlMapper = new SqlSessionFactoryBuilder().build(reader); 9 } 10 11 @Test 12 public void test1() { 13 SqlSession session = sqlMapper.openSession(); 14 //第一种方法,直接调用,名字要和<mapper namespace="mapper.StudentMapper">匹配 15 try { 16 Student student = (Student) session.selectOne("mapper.StudentMapper.get", "ttt"); 17 System.out.println(student); 18 } finally { 19 session.close(); 20 } 21 } 22 23 @Test 24 public void test2() { 25 SqlSession session = sqlMapper.openSession(); 26 //第二种方法,通过接口,mapper名字需要和<mapper namespace="mapper.StudentMapper">匹配 27 try { 28 StudentMapper studentMapper = session.getMapper(StudentMapper.class); 29 Student student = studentMapper.get("ttt"); 30 System.out.println(student); 31 } finally { 32 session.close(); 33 } 34 } 35 }
StudentMapper.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 3 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 4 <mapper namespace="mapper.StudentMapper"> 5 <resultMap type="Student" id="StudentResultMap"> 6 <id property="id" column="id" /> 7 <result property="name" column="name" /> 8 <result property="password" column="password" /> 9 <result property="sex" column="sex" /> 10 <result property="college" column="college" /> 11 <collection property="scores" column="sid" javaType="ArrayList" ofType="Score" resultMap="Score.ScoreResultMap" /> 12 </resultMap> 13 <select id="get" parameterType="String" resultMap="StudentResultMap"> 14 SELECT * FROM sc_student st LEFT OUTER JOIN sc_score sc ON st.id=sc.sid WHERE st.id=#{id} 15 </select> 16 <insert id="insert" parameterType="Student"> 17 insert into sc_student (id,name,password,sex,college) values 18 (#{id},#{name},#{password},#{sex},#{college}) 19 </insert> 20 <update id="update" parameterType="Student"> 21 UPDATE sc_student SET id=#{id},name=#{name},password=#{password}, 22 sex=#{sex},college=#{college} WHERE id=#{id} 23 </update> 24 <delete id="delete" parameterType="Student"> 25 DELETE FROM sc_student WHERE id = #{id} 26 </delete> 27 </mapper>
mapper/StudentMapper.java
1 package mapper; 2 3 import entity.Student; 4 5 public interface StudentMapper { 6 //@Select("SELECT * FROM blog WHERE id = #{id}") //不写mapper可以用这种方式代替,没试过。 7 public Student get(String id); 8 }
Configuration.xml
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 3 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 4 <configuration> 5 <!-- 用于建立会话的变量,可以从文件中读取 --> 6 <properties resource="config/config.properties"> 7 <property name="driver" value="com.mysql.jdbc.Driver" /> 8 <property name="url" value="jdbc:mysql://localhost:3306/test" /> 9 <property name="username" value="root" /> 10 <property name="password" value="asd123" /> 11 </properties> 12 13 <!-- 可以不设置 --> 14 <!-- <setting></setting> --> 15 16 <typeAliases> 17 <typeAlias alias="Student" type="entity.Student"></typeAlias> 18 <typeAlias alias="Score" type="entity.Score"></typeAlias> 19 </typeAliases> 20 <environments default="development"> 21 <environment id="development"> 22 <transactionManager type="JDBC" /> 23 <dataSource type="POOLED"> 24 <property name="driver" value="${driver}" /> 25 <property name="url" value="${url}" /> 26 <property name="username" value="${username}" /> 27 <property name="password" value="${password}" /> 28 </dataSource> 29 </environment> 30 </environments> 31 <mappers> 32 <mapper resource="mapper/StudentMapper.xml" /> 33 <mapper resource="entity/ScoreMapper.xml" /> 34 </mappers> 35 </configuration>