springboot(四、springboot+mybatis)

来源:互联网 发布:电子滚动屏幕软件 编辑:程序博客网 时间:2024/05/17 01:32

1、pom.xml中的jar包


这里面重要的是mybatis-spring-boot-starter这个包,目前进行的是简单的数据库访问  不需要导入那么多不必要的jar包

2、application.properties文件里面写访问数据库url还有驱动等等信息,springboot 自带DataSourceAutoConfiguration会自动读取这个文件

3、testMapper.xml(放在resource下)

<?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="com.example.dao.TestDao" >    <select id="obbm"  resultType="com.example.entity.TestEntity">select id,biid from  obbm   </select></mapper>

4、dao层(这里面注解不要忘记)

@Mapperpublic interface TestDao { List<TestEntity>  obbm();}

5、service层

public interface TestService {public List<TestEntity> testService();}

6、serviceImpl层

@Servicepublic class TestServiceimpl implements TestService{@Autowiredprivate TestDao td;@Overridepublic List<TestEntity> testService() {System.err.println("service......");return td.obbm();}}

7、controller层

@RestControllerpublic class Hello {@Autowiredprivate TestService ts;@RequestMapping("test")public List<TestEntity> datasource1(){ System.err.println("进入controller");return ts.testService();}}

8、入口(这里面@MapperScan需要写在入口扫描,写在controller是不行的)

@SpringBootApplication@MapperScan("com.example.dao.TestDao")public class DatasourcesApplication {public static void main(String[] args) {SpringApplication.run(DatasourcesApplication.class, args);}}

9、总体的结构




原创粉丝点击