11.MyBatis對映檔案
阿新 • • 發佈:2020-12-12
技術標籤:MyBatis學習筆記mybatis
多個對映檔案
- 在 Configuration XML 裡可以配置多個對映檔案
在mybatis-config.xml裡面可以配置多個mapper,可以減少混亂。
<?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> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://127.0.0.1/af_example?useUnicode=true&characterEncoding=UTF-8"/> <property name="username" value="root"/> <property name="password" value="a1b2c3"/> </dataSource> </environment> </environments> <mappers> <mapper resource="mybatis-mapper-student.xml"/> <mapper resource="mybatis-mapper-teacher.xml"/> </mappers> </configuration>
mybatis-mapper-student.xml
<?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="af.student"> <select id="get" resultType="my.Student"> select * from student where id = #{id} </select> </mapper>
mybatis-mapper-teacher.xml
<?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="af.teacher"> <select id="list" resultType="hashmap"> select * from teacher order by id ASC </select> </mapper>
註解方式的Mapper
- 除了xml方式的mapper,Mapper也可以用Java程式碼的方式來定義
- 步驟 :
1.定義一個介面,對應於 Mapper
新增一些方法
添加註解,定義SQL和引數
方法的引數,方法的返回值型別
2.在mybatis-config.xml裡註冊
3.在程式碼裡呼叫
- 註解方式寫的mapper優點:簡潔一些,引數型別和返回值可以自動提示
- 註解方式寫的mapper缺點:不適用於複雜的SQL(比如太長的SQL,要換行書寫)
1.新增介面類mapper StudentMapper.java
package my;
import org.apache.ibatis.annotations.Select;
import my.Student;
public interface StudentMapper
{
@Select("SELECT * FROM student WHERE id = #{id}")
Student getStudent(int id);
}
我們來與xml形式的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="af.student">
<select id="getStu" resultType="my.Student">
select * from student where id = #{id}
</select>
</mapper>
介面類裡面的getStudent對應xml裡面的id,引數id對應sql裡面的引數id,Student對應xml裡面的resultType。
再新增另一個mapper TeacherMapper.java
package my;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.Select;
public interface TeacherMapper
{
@Select("SELECT * FROM teacher ORDER BY ID ASC ")
List<Map> getList();
}
2.在mybatis-config.xml裡註冊mapper
<?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>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1/af_example?useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="a1b2c3"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper class="my.StudentMapper"/>
<mapper class="my.TeacherMapper"/>
</mappers>
</configuration>
3.測試
package my;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
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 Test
{
public static void main(String[] args) throws Exception
{
// 建立 SqlSessionFactory
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
// 以註解方式定義的 Mapper
try (SqlSession session = sqlSessionFactory.openSession()) {
StudentMapper mapper = session.getMapper(StudentMapper.class);
Student stu = mapper.getStudent(20190001);
if(stu != null)
System.out.println("找到學生記錄");
}
try (SqlSession session = sqlSessionFactory.openSession()) {
TeacherMapper mapper = session.getMapper(TeacherMapper.class);
List<Map> rows = mapper.getList();
if(rows != null)
System.out.println("找到教師記錄: " + rows.size());
}
System.out.println("Exit");
}
}
4.結果