1.springboot自动扫描application.properties,不需要配置,扫描自定义配置文件,只需要写一个类,加上@Configuration+@PropertySource("")就可以,里面不需要任何内容,需要获取时,用@Value("${key}")获取。当然配置文件要在classpath下,(https://blog.youkuaiyun.com/qq_31351071/article/details/81006760) 如:
@Configuration
@PropertySource("classpath:a.properties")
public class TestConfig1 {
}
@Value("${a}")
private int a;
2.controller层中:
@Controller和@RestController的区别:@Controller标识:返回前端页面,要返回数据,在@RequestMapper下加上@ResponseBody;@RestController标识:返回数据。如:
@RequestMapping("/fa")
@ResponseBody
public List<User> findAll() {
return user.findAll();
}
3.@ConfigurationProperties(prefix = “test”):自动扫描application.yml(或者properties),将配置文件的属性值自动赋值到Bean中,可以操作。注意:一定要带上get/set方法。
//可以是@Configuration(其底部有@Component)
@Component
@ConfigurationProperties(prefix = "test")
public class TestBean1 {
private String b;
private String c;
public String getB() {
return b;
}
public void setB(String b) {
this.b = b;
}
public String getC() {
return c;
}
public void setC(String c) {
this.c = c;
}
}
//也可以调用这个类
@EnableConfigurationProperties({TestBean1.class})
public class DruidConfig {
@Autowired
private TestBean1 properties;
}
@ConfigurationProperties(prefix = “test”) 不仅可以自动装配application.properties,也可以装配自定义配置文件 ,只需要和
@PropertySource(“classpath:a.properties”)配合使用即可,如:
@Configuration
@PropertySource("classpath:a.properties")
@ConfigurationProperties(prefix = "test")
public class TestBean1 {
private String b;
public String getB() {
return b;
}
public void setB(String b) {
this.b = b;
}
}
4.关于Controller接收前端页面的参数:
1>.@PathVariable:即url/{param}这种形式
2>.@RequestParam:般我们使用该注解来获取多个参数,在()内写入需要获取参数的参数名即可,一般在PUT,POST中比较常用。
3>.@RequestBody:该注解和@RequestParam殊途同归,我们使用该注解将所有参数转换,在代码部分在一个个取出来,也是目前我使用到最多的注解来获取参数(因为前端不愿意一个一个接口的调试)例如下代码:
@PostMapping("/createUserByMap")
public void createUserByMap(@RequestBody Map<String,Object> reqMap){
String tel = reqMap.get("tel").toString();
String pwd = reqMap.get("pwd").toString();
userService.createUser(tel,pwd);
}
4>.使用对象获取
/**
* 添加用户2
* @param userInfo
*/
@PostMapping("/createUser2")
public void createUser2(UserInfo userInfo){
userService.createUser(userInfo.getTel(),userInfo.getPassWord());
}
5>.参数与传的参数一致即可,如:
http://localhost:987/fu1?ids=2
@RequestMapping("/fu1")
@ResponseBody
public User findUserById1(String ids) {
String id="2";
return user.findById(ids);
}
5.springboot+mybatis多表查询
1>注解:
//一对一查询
public interface UserRepository {
@Select("SELECT * FROM `user` where id = #{id}")
@Results({
@Result(property = "address", column = "address_id",
one = @One(select = "com.kingboy.repository.address.AddressRepository.findAddressById"))
})
User findUserWithAddress(Long id);
}
//一对多查询
public interface UserRepository {
/**
* 查询带有车信息的用户===============演示一对多(关于多对多其实就是两个一对多组成)
*/
@Select("SELECT * FROM `user` WHERE id = #{id}")
@Results({
@Result(property = "cars", column = "id",
many = @Many(select = "com.kingboy.repository.car.CarRepository.findCarByUserId"))
})
User getUserWithCar(Long id);
}
2>xml文件:
//1
<resultMap id="userResultMap" type="User">
<id property="id" column="user_id" />
<result property="username" column="user_name"/>
<result property="password" column="hashed_password"/>
</resultMap>
//2
//select语句
<!-- Very Complex Statement -->
<select id="selectBlogDetails" resultMap="detailedBlogResultMap">
select
B.id as blog_id,
from Blog B
left outer join Author A on B.author_id = A.id
left outer join Post P on B.id = P.blog_id
left outer join Comment C on P.id = C.post_id
left outer join Post_Tag PT on PT.post_id = P.id
left outer join Tag T on PT.tag_id = T.id
where B.id = #{id}
</select>
<!-- 超复杂的 Result Map -->
<resultMap id="detailedBlogResultMap" type="Blog">
<constructor>
<idArg column="blog_id" javaType="int"/>
</constructor>
<result property="title" column="blog_title"/>
<!-- 一对一 -->
<association property="author" javaType="Author">
<id property="id" column="author_id"/>
<result property="username" column="author_username"/>
<result property="password" column="author_password"/>
<result property="email" column="author_email"/>
<result property="bio" column="author_bio"/>
<result property="favouriteSection" column="author_favourite_section"/>
</association>
<!-- 一对多 -->
<collection property="posts" ofType="Post">
<id property="id" column="post_id"/>
<result property="subject" column="post_subject"/>
<association property="author" javaType="Author"/>
<collection property="comments" ofType="Comment">
<id property="id" column="comment_id"/>
</collection>
<collection property="tags" ofType="Tag" >
<id property="id" column="tag_id"/>
</collection>
<discriminator javaType="int" column="draft">
<case value="1" resultType="DraftPost"/>
</discriminator>
</collection>
</resultMap>
总结:注解的@Results==<requestMap></requestMap>,sql语句怎样写都行(连接查询,无论单表还是多表)
注解的@Requests和@Request就是提供一个容器(场所),存放数据,和sql语句的实现无关(就是说,sql语句无论怎样写,只看结果)
,如左连接查询,即使一条数据对应多条数据,就可以用注解many或者xml的collection。
springboot知识小总结1:https://blog.youkuaiyun.com/weixin_43075298/article/details/88662097
不定时更新!