1.1 基本使用
1.1.1 常用注解
| 注解 | 描述 |
|---|---|
| @Insert | 實作新增 |
| @Update | 實作更新 |
| @Delete | 實作洗掉 |
| @Select | 實作查詢 |
| @Result | 實作結果集封裝 |
| @Results | 可以與 @Result 一起使用,封裝多個結果集 |
| @One | 實作一對一結果集封裝 |
| @Many | 實作一對多結果集封裝 |
1.1.2 MyBatis 增刪改查
? 物體類
public class Student {
private Long sId;
private String sName;
private Long sAge;
private String sSex;
// set and get
}
? DAO
/**
* Created with IntelliJ IDEA.
*
* @author Demo_Null
* @date 2020/9/9
* @description Mybatis 注解
*/
public interface StudentDao {
@Select("select * from student")
public List<Student> findAll();
@Select("select * from where s_id = #{id} ")
public Student findOne(Long id);
@Insert("insert into student values (#{sId}, #{sName}), #{sAge}, #{sSex}")
public int insert(Student student);
@Update("update from student set s_id = #{sId}, s_name = #{sName}, s_age = #{sAge}, s_sex = #{sSex}")
public int update(Student student);
@Delete("delete from student where s_id = #{id} ")
public int delete(Long id);
}
? 組態檔
<typeAliases>
<package name="com.software.mybatis.entity"/>
</typeAliases>
<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://localhost:3306/db"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<mappers>
<!-- 指定介面所在的包 -->
<package name="com.software.mybatis.dao"/>
<!-- 指定介面 -->
<mapper class="com.software.mybatis.dao.StudentDao"/>
</mappers>
? 測驗
/**
* Created with IntelliJ IDEA.
*
* @author Demo_Null
* @date 2020/9/9
* @description
*/
public class MybatisDemo {
private StudentDao studentDao;
@Before
public void before() throws IOException {
// 加載核心組態檔
InputStream resourceAsStream = Resources.getResourceAsStream("mybatis.xml");
// 獲得 sqlSession 工廠物件
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
// 獲得 sqlSession 物件
SqlSession sqlSession = sqlSessionFactory.openSession();
// 獲取 mapper
studentDao = sqlSession.getMapper(StudentDao.class);
}
@Test
public void TestA() throws IOException {
List<Student> studentList = studentDao.findAll();
System.out.println(studentList);
}
}

??我們可以看到,明明結果已經查詢出來了,為什么列印出來卻是空的,這個是因為屬性名和列名不一致造成的,類似于我們這種 sId ? s_id 可以打開駝峰之自動轉換,如果二者之間沒有任何聯系就需要使用 @Results 一一映射,
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
@Select("select * from student")
@Results(value = {
@Result(property = "sId", column = "s_id"),
@Result(property = "sName", column = "s_name"),
@Result(property = "sAge", column = "s_age"),
@Result(property = "sSex", column = "s_sex")
})
public List<Student> findAll();

1.2 復雜映射
1.2.1 注解詳解
| 注解 | 說明 |
|---|---|
| @Results | 代替的是標簽該注解中可以使用單個@Result注解,也可以使用@Result集合 使用格式:@Results({@Result(),@Result()})或@Results(@Result()) |
| @Result | 代替了標簽和標簽 @Result中屬性介紹: column:資料庫的列名 property:需要裝配的屬性名 one:需要使用的@One 注解(@Result(one=@One)())) many:需要使用的@Many 注解(@Result(many=@many)())) |
| @One(一對一) | 代替了標簽,是多表查詢的關鍵,在注解中用來指定子查詢回傳單一物件, @One注解屬性介紹: select:指定用來多表查詢的sqlmapper使用格式:@Result(column="",property="".one=@One(select=")) |
| @Many(一對多) | 代替了標簽,是是多表查詢的關鍵,在注解中用來指定子查詢回傳物件集合, 使用格式:@Result(property="".column=",many=@Many(select=")) |
1.2.2 一對一
? 物體類
public class Student {
private Long sId;
private String sName;
private Long sAge;
private String sSex;
private Class class;
// set and get
}
public class Class {
private Long cId;
private String cName;
private String cAddr;
// set and get
}
? DAO
public interface ClassDao {
@Select("select * from class where c_id = #{id} ")
public Class findById(Long id);
}
public interface StudentDao {
@Select("select * from student")
@Results({
@Result(property = "sId", column = "s_id"),
@Result(property = "sName", column = "s_name"),
@Result(property = "sAge", column = "s_age"),
@Result(property = "sSex", column = "s_sex"),
// 類似于先查詢出 student 然后拿 c_id 再去查 class
@Result(property = "class", column = "c_id", javaType = Class.class,
one = @One(select = "com.software.mybatis.dao.ClassDao.findById"))
})
public List<Student> findAll();
}
1.2.3 一對多
? 物體類
public class Student {
private Long sId;
private String sName;
private Long sAge;
private String sSex;
private Long cId;
// set and get
}
public class Class {
private Long cId;
private String cName;
private String cAddr;
private List<Student> students;
// set and get
}
? DAO
public interface StudentDao {
@Select("select * from where c_id = #{id} ")
public List<Student> findByCid(Long id);
}
public interface ClassDao {
@Select("select * from class")
@Results({
@Result(property = "cId", column = "c_id"),
@Result(property = "cName", column = "c_name"),
@Result(property = "cAddr", column = "c_addr"),
// 類似于先查詢 class 然后使用 c_id 查詢 student
@Result(property = "students", column = "c_id", javaType = List.class,
many = @Many(select = "com.software.mybatis.dao.StudentDao.findByCid")),
})
public List<Class> findAll();
}
1.2.4 多對多
? 物體類
public class Course {
private Integer cId;
private String cName;
private List<Student> students;
// set and get
}
public class Student {
private Integer sId;
private String sName;
private Long sAge;
private String sSex;
private List<Course> courses;
// set and get
}
? DAO
public interface CourseDao {
@Select("select * from course c, s_c sc where c.c_id = sc.c_id and sc.s_id = #{id} ")
public List<Course> findBySid(Long id);
}
public interface StudentDao {
@Select("select * from student")
@Results({
@Result(property = "sId", column = "s_id"),
@Result(property = "sName", column = "s_name"),
@Result(property = "sAge", column = "s_age"),
@Result(property = "sSex", column = "s_sex"),
// 類似于先查詢 student 然后連接查詢 course 和 s_c 表條件為 c.c_id = sc.c_id and s_id = sc.s_id
@Result(property = "Courses", column = "s_id", javaType = List.class,
many = @Many(select = "com.software.mybatis.dao.CoursesDao.findBySid"))
})
public List<Student> findAll();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/3238.html
標籤:其他
下一篇:GitLab介紹及安裝
