笔记54 Mybatis快速入门(五)

一、Mybatis中注解的使用

1.XML方式的CRUD

新增加接口CategoryMapper ,并在接口中声明的方法上,加上注解对比配置文件Category.xml,其实就是把SQL语句从XML挪到了注解上来。

CategoryMapper.java

 1 package mybatis.mapper;
 2 
 3 import java.util.List;
 4 
 5 import org.apache.ibatis.annotations.Delete;
 6 import org.apache.ibatis.annotations.Insert;
 7 import org.apache.ibatis.annotations.Select;
 8 import org.apache.ibatis.annotations.Update;
 9 
10 import mybatis.pojo.Category;
11 
12 public interface CategoryMapper {
13     @Insert("insert into category (name) values (#{name})")
14     public int add(Category category);
15 
16     @Delete("delete from category where id= #{id}")
17     public void delete(int id);
18 
19     @Select("select * from category where id= #{id}")
20     public Category get(int id);
21 
22     @Update("update category set name=#{name} where id=#{id}")
23     public int update(Category category);
24 
25     @Select("select * from category")
26     public List<Category> list();
27 }

在mybatis-config.xml中增加映射:

1 <mapper class="mybatis.mapper.CategoryMapper"/>

测试:

 1 package mybatis.annotation;
 2 
 3 import java.io.IOException;
 4 import java.io.InputStream;
 5 import java.util.List;
 6 
 7 import org.apache.ibatis.session.SqlSession;
 8 import org.apache.ibatis.session.SqlSessionFactory;
 9 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
10 
11 import mybatis.mapper.CategoryMapper;
12 import mybatis.pojo.Category;
13 
14 public class testCRUD {
15     public static void main(String[] args) throws IOException {
16         String resource = "mybatis-config.xml";
17         InputStream inputStream = org.apache.ibatis.io.Resources.getResourceAsStream(resource);
18         SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
19         SqlSession session = sqlSessionFactory.openSession();
20         CategoryMapper categoryMapper = session.getMapper(CategoryMapper.class);
21         add(categoryMapper);
22         // listAll(categoryMapper);
23         session.commit();
24         session.close();
25 
26     }
27 
28     private static void update(CategoryMapper mapper) {
29         Category category = mapper.get(0);
30         category.setName("修改了的Category名称");
31         mapper.update(category);
32         listAll(mapper);
33     }
34 
35     private static void delete(CategoryMapper mapper) {
36         mapper.delete(2);
37         listAll(mapper);
38     }
39 
40     private static void add(CategoryMapper mapper) {
41         Category category = new Category();
42         category.setName("新增的Category");
43         mapper.add(category);
44         listAll(mapper);
45     }
46 
47     private static void get(CategoryMapper mapper) {
48         Category category = mapper.get(1);
49         System.out.println(category.getName());
50     }
51 
52     private static void listAll(CategoryMapper mapper) {
53         List<Category> cs = mapper.list();
54         for (Category c : cs) {
55             System.out.println(c.getName());
56         }
57     }
58 }

2.一对多

①查询所有Category,通过@Select注解获取Category类本身。@Results 通过@Result和@Many中调用ProductMapper.listByCategory()方法相结合,来获取一对多关系。

1     @Select("select * from category")
2     @Results({ @Result(property = "id", column = "id"),
3                @Result(property = "products", javaType = List.class, column = "id", many = @Many(select = "mybatis.mapper.ProductMapper.listByCategory")) })
4     public List<Category> list2();

②新增接口ProductMapper
注解@Select用于根据分类id获取产品集合
@Select(" select * from product_ where cid = #{cid}")

 1 package mybatis.mapper;
 2 
 3 import java.util.List;
 4 
 5 import org.apache.ibatis.annotations.Select;
 6 
 7 import mybatis.pojo.Product;
 8 
 9 public interface ProductMapper {
10     @Select("select * from product where cid=#{cid}")
11     public List<Product> listByCategory(int cid);
12 }

③添加ProductMapper和CategoryMapper的映射

1 <mapper class="mybatis.mapper.CategoryMapper"/>
2 <mapper class="mybatis.mapper.ProductMapper"/>

④结果:

3.多对一

4.多对多

5.动态SQL语句

猜你喜欢

转载自www.cnblogs.com/lyj-gyq/p/9241110.html
54
今日推荐