栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java > SpringBoot

SpringBoot-16-之整合MyBatis-xml篇+单元测试

SpringBoot 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot-16-之整合MyBatis-xml篇+单元测试

0.项目结构
java
    dao    |---SwordDao
    entity
    |---Sword
resources
    mapper    |---Sword.xml
    application.yml

1.application.yml
#坑点0 配置mybatis的xml位置mybatis:
  mapper-locations: classpath:mapper/*.xml
2.新建dao文件夹,新建dao接口:SwordDao.java
public interface SwordDao {

    List findALL();

    Sword findByName(@Param("name") String name);    //坑点1 java没有保存形参的记录,所以多参数用户@Param("name")起名字,不然无法识别
    int insert(@Param("name") String name,
                @Param("atk") Integer atk,
                @Param("hit") Integer hit,
                @Param("crit") Integer crit,
                @Param("attr_id") Integer attr_id,
                @Param("type_id") Integer type_id
    );
}
3.在resources下新建mapper文件夹,再建Sword.xml文件,为dao层提供SQL语句

    
    
      insert into sword(name,atk,hit,crit,attr_id,type_id)
      values (#{name},#{atk},#{hit},#{crit},#{attr_id},#{type_id})    
    
    
        SELECT*FROM sword WHERe NAME=#{name}    

    
        SELECT*FROM sword    
4.将dao添加扫包范围:com.toly1994.toly_mybatis.TolyMybatisApplication
//坑点5:将dao添加扫包范围@MapperScan(basePackages = {"com.toly1994.toly_mybatis.mapper","com.toly1994.toly_mybatis.dao"})

5.单元测试:test文件夹下com.toly1994.toly_mybatis.dao.SwordDaoTest
@RunWith(SpringRunner.class)@SpringBootTestpublic class SwordDaoTest {    @Autowired
    private SwordDao mSwordDao;    @Test
    public void findALL() {
        List all = mSwordDao.findALL();
        System.out.println(all.get(5));//Sword(id=6, name=风跃, atk=9020, hit=10, crit=10, attr_id=2, type_id=2)
    }    @Test
    public void findByName() {
        Sword 赤凰 = mSwordDao.findByName("赤凰");
        System.out.println(赤凰);//Sword(id=13, name=赤凰, atk=0, hit=100, crit=5, attr_id=1, type_id=2)
    }    @Test
    public void insert() {        int insert = mSwordDao.insert("尤恨", 4000, 100, 100, 1, 1);
        System.out.println(insert);//1
    }
}

6.联合查询:可能会疑惑attr_id和type_id是干嘛的,其实是两张关联表

联合查询.png

修改实体类两个字段:
@Data//=@Getter +@Setterpublic class Sword {    private Integer id;    private String name;    private Integer atk;    private Integer hit;    private Integer crit;    private String type_name;//改为String
    private String attr;//改为String}
修改查询所有的SQL语句
    
       SELECT id,name,atk,hit,crit,type_name,attr FROM sword AS s
      INNER JOIN sword_type AS t ON s.type_id = t.type_id
      INNER JOIN sword_attr AS a ON s.attr_id = a.attr_id;
    
测试:可见两张表和主表连在一起了
    @Test
    public void findALL() {
        List all = mSwordDao.findALL();
        System.out.println(all.get(5));        //Sword(id=6, name=风跃, atk=9020, hit=10, crit=10, type_name=仙界, attr=木)
    }



作者:张风捷特烈
链接:https://www.jianshu.com/p/e7b98cc68a80


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/235218.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号