Java(Spring+Mybatis+SpringMVC)学习笔记

项目pom.xml格式为:

classpath指向resources.

resources下文件为:

mapper/
spring/
jdbc.properties
logback.xml
mybatis-config.xml

mybatis-config.xml是mybatis的配置文件,内容:

logback.xml是logback日志模块的配置文件:

jdbc.properties是jdbc相关配置模板,提供给mybatis。内容:

spring/里有3个文件,分别是:

spring-dao.xml 把db连接池封装进mybatis,然后把mybatis封装进spring里;
spring-service.xml 配置service,包含事务处理;
spring-web.xml  配置springMVC;

spring-dao.xml内容,注意这里有2个连接池版本的配置,有一个c3p0的,有一个druid的:

spring-service.xml内容:

需要注意service层如果有事务,必须抛出RuntimeException才会回滚,使用Exception是不会打断的。

spring-web.xml内容:

 

mapper内文件内容:

@Controller,@Service,@Component都属于spring的支持;

@Autowired是spring的方式,对应j2EE的@Resource,@Inject

对于接口不需要注解标注,只对实现类做标注。

 

Dao对应Mybatis有多个参数时,使用@Param标注参数名称,不然在mapper文件里不认。

web的controller要接收参数时,使用@PathVariable来标注参数,不然收不到。读取cookie用@CookieValue标注。如果要指定返回json或xml,在@RequestMapping里指定produces = {“application/json;charset=UTF-8”},然后增加@ResponseBody标注。

webapp/WEB-INF/web.xml内容:

从web.xml开始,将resources/spring/里的所有配置文件加载,然后在spring-dao.xml里会把mybatis加载进去,最后都被加载到springMVC中。

SQL插入unicode字符(表情包)

Mysql:
字段编码设置为utf8mb4;

sql server:
1、table定義字段時要用nvarchar
2、insert时指定为unicode(N)
如:
insert into table values (N ‘content ‘)
这里,N表示采用UNICODE编码方式。有N,插入数据库里的是unicode编码(无论是汉字,还是字母,统一用两个字节表示)的数据,这样在不同的系统环境下就会出现乱码,只对字段类型是nvarchar/ntext/nchar的字段有效。
若无N,则按非unicode编码来存储,即汉字是双字节存储,字母单字节存储)。

一个文件夹最多能放多少文件?

FAT32:
Maximum number of files: 268,173,300
Maximum number of files per directory: 216 – 1 (65,535)
Maximum file size: 2 GiB – 1 without LFS, 4 GiB – 1 with
NTFS:
Maximum number of files: 232 – 1 (4,294,967,295)
Maximum file size
Implementation: 244 – 26 bytes (16 TiB – 64 KiB)
Theoretical: 264 – 26 bytes (16 EiB – 64 KiB)
Maximum volume size
Implementation: 232 – 1 clusters (256 TiB – 64 KiB)
Theoretical: 264 – 1 clusters
ext2:
Maximum number of files: 1018
Maximum number of files per directory: ~1.3 × 1020 (performance issues past 10,000)
Maximum file size
16 GiB (block size of 1 KiB)
256 GiB (block size of 2 KiB)
2 TiB (block size of 4 KiB)
2 TiB (block size of 8 KiB)
Maximum volume size
4 TiB (block size of 1 KiB)
8 TiB (block size of 2 KiB)
16 TiB (block size of 4 KiB)
32 TiB (block size of 8 KiB)
ext3:
Maximum number of files: min(volumeSize / 213, numberOfBlocks)
Maximum file size: same as ext2
Maximum volume size: same as ext2
ext4:
Maximum number of files: 232 – 1 (4,294,967,295)
Maximum number of files per directory: unlimited
Maximum file size: 244 – 1 bytes (16 TiB – 1)
Maximum volume size: 248 – 1 bytes (256 TiB – 1)

SQL Server 2008删除db日志

USE AdventureWorks2008R2;
GO
— Truncate the log by changing the database recovery model to SIMPLE.
ALTER DATABASE AdventureWorks2008R2
SET RECOVERY SIMPLE;
GO
— Shrink the truncated log file to 1 MB.
DBCC SHRINKFILE (AdventureWorks2008R2_Log, 1);
GO
— Reset the database recovery model.
ALTER DATABASE AdventureWorks2008R2
SET RECOVERY FULL;
GO
from : http://msdn.microsoft.com/en-us/library/ms189493.aspx

Spring Security的一些资料

Spring Security Authentication Provider

https://dzone.com/articles/spring-security-custom

http://blog.solidcraft.eu/2011/03/spring-security-by-example-user-in.html

http://www.mincoder.com/article/3505.shtml

Spring Security 4 Role Based Login Example

Spring Security 4 Hibernate Role Based Login Example

Spring Security 4 Hibernate Integration Annotation+XML Example

Spring Security 4 Hibernate Integration Annotation+XML Example

用Java代码来代替xml配置spring

Since Spring 3, Java configuration (@Configuration) has been moved into spring-core and has caught my attention. This is a quick sample of how to convert an existing servlet.xml file into a java config file extending WebMvcConfigurerAdapter.

Beginning xml

1. Create the configuration class, I like to create a “config” package with a class named appConfig.java
2. Add @Configuration, this will let spring know this contains bean definitions.

3. Add @EnableWebMVC, this is the same as <mvc:annotation-driven/>

4. Add @ComponentScan(basePackages = {“com.luckyryan.sample”}), this is the same as <context:component-scan base-package=”com.luckyryan.sample”/>

5. Extend the class to use WebMvcConfigurerAdapter. This adds stub implementations from the WebMvcConfigurer interface which is used by @EnableWebMVC. It also gives us a chance to override resources and the default handler.

6. Declare our static resources. I added cache to the java config but it’s not required.

7. Set default servlet handler, this is the same as <mvc:default-servlet-handler/>

8. Add bean for InternalResourceViewResolver

9. Update the servlet declaration in web.xml for the new config class and annotation conf. Note: this replaces the need for <context:annotation-config/> which was not featured in the servlet.xml example.

原文地址:
https://www.luckyryan.com/2013/02/07/migrate-spring-mvc-servlet-xml-to-java-config/