java怎么把Controller层的数据怎么传给dao层
1、第一步:dao层是数据库连接层,就是负责对数据的增删改查的,这里使用的springboot项目,在pom.xml引入mybatis和mysql的依赖,代码如下:
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
2、第二步:在application.properties里面进行数据库相关的配置,简单配置如下:
spring.datasource.driverClassName = com.mysql.cj.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
spring.datasource.username = root
spring.datasource.password = root
mybatis.mapper-locations: classpath:mapper/*.xml
mybatis.type-aliases-package=com.demo.deml

3、第三步:新建包名,并在启动类上配置mybatis扫描的路径,如图所示:

4、第四步:编写controller和dao代码,并配置好mapper.xml,代码如下:
// controller
@PostMapping("/querycount")
String queryCount(){
Integer c = mapper.count("test");
return c.toString();
}
// dao interface
Integer count(@Param("pwd") String pwd);

5、第五步:基本代码完成,现在就是controller往dao传参了,首先要自动注入dao层,代码如下:
@Autowired
DemoMapper mapper;

6、第六步:往到层传入参数 test,在请求的sql里面看到参数已经结束了,如图:

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。