long blogs

进一步有进一步惊喜


  • Home
  • Archive
  • Tags
  •  

© 2025 long

Theme Typography by Makito

Proudly published with Hexo

spring笔记

Posted at 2019-07-15 笔记 spring 

零碎知识点

1、注解配置和xml配置的优先级
  • xml 配置优先于注解配置。如果同时有xml和注解,则xml中会覆盖掉注解中的配置。
2、bean的相关知识

1、bean的属性

(1)class bean里面的所连接的类

(2)name 这个bean的标识

(3)scope bean的对象的作用域

​ bean的作用域:

singleton 单例,只要调用该bean的都是得到同一个对象。默认的属性

prototype 原型, 每次获得该bean都得到一个新的bean

request http请求会产生一个新的实例bean.仅适用于WebApplicationContext环境

session 同一个session共享一个bean。不同的session使用不同的bean.用于WebApplicationContext环境

global-session Portlet应用环境。仅适用于WebApplicationContext环境。

2、bean的生命周期

(1)Bean的定义

xml中配置init-method 和destroy-method参数。init-method 实例化的时候调用该方法。destroy-method 容器移除bean之后,调用该方法。

在beans标签里面默认的初始化和销毁方法

1
2
3
4
5
6
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
default-init-method="init"
default-destroy-method="destroy">

(2)Bean的初始化

(3)Bean的使用

(4)Bean的销毁

SpringMVC Controller

1、在dispatcher-servlet.xml中配置

1
2
<!-- 扫描控制器的所在的包 -->
<context:component-scan base-package="Controller"/>

2、controller配置路径的post、get请求。

在@RequestMapping()中含有一些参数可一个配置路径

  • method 请求的方法
    例如:
    (1)post请求:@RequestMapping(path="/login",method = RequestMethod.POST)
    (2)get请求的简写:@GetMapping(path = "/login")但是使用相同的方法简写post请求就出现错误了。

3、controller Model

可以使用Model#addAttribute(K,V) 来设置传回去的值。然后在jsp页面中使用${K}获得model中V的值。

4、controller 获得页面传过来的值

直接在函数的入口参数中声明便可以获得相应的值

1
2
3
4
5
6
@RequestMapping(path = "/login",method = RequestMethod.POST)
public String login(Model model,String account,String password ) {
model.addAttribute("account",account);
model.addAttribute("password",password);
return "info";
}

其中的info是视图的名称。会将model传递到info.jsp中。

SpringMVC View

1、在view中使用model中的数据。

  • 在info.jsp中显示用户从index.jsp中传过来用户名和密码
1
2
3
4
5
6
7
8
<table>
<tr>
<td>账号:</td><td>${account}</td>
</tr>
<tr>
<td>密码:</td><td>${password}</td>
</tr>
</table>
2、url路径映射

@RequestMapping(“/a/{ownId})
获得ownId的值
@PathvVariable String ownId

1
2
@RequestMapping(value = "/{recordId}",method = RequestMethod.GET)
public PublishRecordDto getRecordById(@PathVariable("recordId") long recordId){

spring boot

1、配置mybatis的坑。

application.properties
自动生成的是有问题的

1
2
spring.datasource.data-username=root
spring.datasource.data-password=root

正确的配置如下

1
2
3
4
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/train
spring.datasource.username=root
spring.datasource.password=root

2、扫描dao的包位置

@MapperScan("com.package..dao")
在dao使用接口类来定义一个dao.全部使用注解。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
**
* Mybatis 的dao 接口
* @author longquanxiao
* @date 2019/7/26
*/
public interface DictionaryDao {

/**
* 插入数据
* @param dictionary 数据字典
* @return 插入的个数
*/
@Insert("INSERT INTO t_dictionary (category_id,name,category,value,remark,status,org_id," +
"created_by,created_time,updated_by,updated_time,version) values" +
"(#{categoryId},#{name},#{category},#{value},#{remark},#{status},#{orgId}," +
"#{createdBy},#{createdTime},#{updatedBy},#{updatedTime},#{version}) ")
int insertUser(Dictionary dictionary);

/**
* 获得所有的字典
* @return
*/
@Select("SELECT * FROM t_dictionary")
@Results(id = "dictionaryMap",value = {
@Result(id = true,column = "category_id",property = "categoryId"),
@Result(column = "name",property = "name"),
@Result(column = "category",property = "category"),
@Result(column = "value",property = "value"),
@Result(column = "remark",property = "remark"),
@Result(property="status",column="status"),
@Result(property="orgId",column="org_id"),
@Result(property="createdBy",column="created_by"),
@Result(property="createdTime",column="created_time"),
@Result(property="updatedBy", column="updated_by"),
@Result(property="updatedTime", column="updated_time"),
@Result(property="version" ,column="version")
})
List<Dictionary> queryAll();
}

Share 

 Previous post: linux安装MySql Next post: maven环境变量配置 

© 2025 long

Theme Typography by Makito

Proudly published with Hexo