1.mybatis整合SpringMVC
•【mybatis-ssm】导入需要的依赖包(pom.xml配置文件);
•【mybatis-ssm】在src/main/profiles/dev/config/下创建database.properties资源文件(这里的数据库连接是采用了连接池);
•【mybatis-ssm】在src/main/resources/spring/下创建spring-datasource.xml配置文件,在此配置文件中设置连接属性(引用database.properties中的值);
•【mybatis-ssm】配置spring-transaction.xml配置文件,主要定义的是所有的业务层的切面方法,同时要配置好切面表达式(这个还不太理解);
•【mybatis-ssm】创建spring-base.xml配置文件,在这个文件中需要考虑好扫描包的问题:扫描所有关于配置的*.properties资源文件,导入spring-mvc.xml配置文件以外的spring-xxx.xml配置文件,扫描dao子类所在的包,
扫描service子类所在的包,启用业务层注解(也可以不设置);
•【mybatis-ssm】修改web.xml配置文件,追加DispatcherServlet配置类,编码过滤器等(1.加载spring-base.xml配置文件,2.设置WEB容器启动的时候实现Spring容器的启动,3.配置SpringMVC的分发处理Servlet,利用此Servlet找到所有的Action(加载spring-mvc.xml配置文件),4配置项目中的编码过滤器);
•【mybatis-ssm】创建spring-mvc.xml配置文件,1.定义控制层的扫描包,2.开启控制层的注解配置模式,3.设置路径前缀后缀匹配,4.为保存在WEB-INF下的所有静态资源文件设置映射访问路径,5.进行拦截器的访问路径的匹配,6.进行所要加载的*.properties配置文件的路径定义,直接通过CLASSPATH加载(这些资源文件的的目录为src/main/resources/com.yootk.message/目录下,具体作用为:页面跳转路径的资源文件pages.properties,提示信息的资源文件message.properties,拦截器规则验证的资源文件validation.properties),7.进行上传文件组件的相关配置,8.定义全局跳转路径的配置(例如错误页跳转);
•【mybatis-ssm】如果要进行Spring与Mybatis整合,整个的项目之中依然需要提供有mybatis.cfg.xml配置文件,将此文件保存在src/main/resources/mybatis/目录之中(在此配置文件中设置类的别名);
•【mybatis-ssm】如果要将Spring与Mybatis整合,就需要创建src/main/resources/spring/spring-mybatis配置文件(在此文件中配置mybatis.cfg.xml配置文件的路径,配置映射文件News.xml配置文件的路径(事实上映射文件会有很多,如果一个一个加载会很麻烦,所以可以在这里配置包.*.xml配置文件配置)),由于增加了一个新的配置文件,所以需要修改spring-base.xml配置文件导入此配置文件;
•【mybatis-ssm】编写INewsDAO接口,此接口的实现子类就不需要写了,直接采用News.xml配置文件(一定要将此映射文件的命名空间改为与INewsDAO同样的名称,SQL语句的id也要与接口的方法的名称一一对应,然后修改spring-mybatis.xml配置文件,追加一个映射配置转换的处理工具类追加DAO接口所在包的扫描);
•【mybatis-ssm】定义INewsService业务层标准接口,定义业务层实现子类;
•【mybatis-ssm】之后编写控制层,跳转路径在上面定义的pages.properties资源文件中定义,在控制层里面写跳转路径的时候直接引用资源文件的映射即可,控制层和显示层的跳转和以往相同。
2.dubbo(RPC框架)与SSM框架整合
【dubbo】项目下一共有三个认证微服务:认证微服务,授权微服务,日志微服务,但是其中有【dubbo-api】模块,【dubbo-util】工具模块,【dubbo-echo-provider】生产模块,【dubbo-echo-consumer】消费模块,【dubbo-test-consumer】测试消费模块,【server-common】公共模块,【authentication】认证模块,【authorization】授权模块,【login】登录日志模块。
• 【dubbo-api】模块:
• vo类(如:Member)
• 业务层接口(如:IMemberService)
• 【authentication】认证模块(提供端):
• 数据层接口(如:IMemberDAO)
• Member.xml文件,在此文件中写dao接口的子类即SQL语句
• 定义业务层的实现子类(如:MemberServiceImpl),@Service注解用dubbo里面的,前提是要定义扫描包
• 程序的启动类
• 【dubbo-test-consumer】测试消费模块:实例化业务层对象,用@Reference注解,调用业务层方法
• 【authorization】授权模块(提供端):
• 数据层接口(IRoleDAO,IActionDAO)
• Role.xml,Action.xml
• 定义业务层的实现子类(RoleAndActionServiceImpl),@Service注解用dubbo里面的,前提是要定义扫描包
• 程序的启动主类
• 【server-common】公共模块:
• src/main/java/com.yootk.server.config.DatabaseConfig.java
package com.yootk.server.config; import com.alibaba.druid.pool.DruidDataSource; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.data.repository.NoRepositoryBean; import javax.sql.DataSource; import java.sql.SQLException; @Configuration @PropertySource("classpath:database.properties") public class DatabaseConfig { @Value("${db.druid.driverClassName}") private String driverClassName ; @Value("${db.druid.url}") private String url ; @Value("${db.druid.username}") private String username ; @Value("${db.druid.password}") private String password ; @Value("${db.druid.maxActive}") private int maxActive ; @Value("${db.druid.minIdle}") private int minIdle ; @Value("${db.druid.initialSize}") private int initialSize ; @Value("${db.druid.maxWait}") private long maxWait ; @Value("${db.druid.timeBetweenEvictionRunsMillis}") private long timeBetweenEvictionRunsMillis ; @Value("${db.druid.minEvictableIdleTimeMillis}") private long minEvictableIdleTimeMillis ; @Value("${db.druid.validationQuery}") private String validationQuery ; @Value("${db.druid.testWhileIdle}") private boolean testWhileIdle ; @Value("${db.druid.testOnBorrow}") private boolean testOnBorrow ; @Value("${db.druid.testOnReturn}") private boolean testOnReturn ; @Value("${db.druid.poolPreparedStatements}") private boolean poolPreparedStatements ; @Value("${db.druid.maxPoolPreparedStatementPerConnectionSize}") private int maxPoolPreparedStatementPerConnectionSize ; @Value("${db.druid.filters}") private String filters ; @Bean("dataSource") public DataSource getDruidDataSource() { DruidDataSource dataSource = new DruidDataSource() ; dataSource.setDriverClassName(this.driverClassName); dataSource.setUrl(this.url); dataSource.setUsername(this.username); dataSource.setPassword(this.password); dataSource.setMaxActive(this.maxActive); dataSource.setMinIdle(this.minIdle); dataSource.setInitialSize(this.initialSize); dataSource.setMaxWait(this.maxWait); dataSource.setTimeBetweenEvictionRunsMillis(this.timeBetweenEvictionRunsMillis); dataSource.setMinEvictableIdleTimeMillis(this.minEvictableIdleTimeMillis); dataSource.setValidationQuery(this.validationQuery); dataSource.setTestWhileIdle(this.testWhileIdle); dataSource.setTestOnBorrow(this.testOnBorrow); dataSource.setTestOnReturn(this.testOnReturn); dataSource.setPoolPreparedStatements(this.poolPreparedStatements); dataSource.setMaxPoolPreparedStatementPerConnectionSize(this.maxPoolPreparedStatementPerConnectionSize); try { dataSource.setFilters(this.filters); } catch (SQLException e) { e.printStackTrace(); } return dataSource ; } }
• src/main/java/com.yootk.server.config.SpringDataRedisConfig.java
package com.yootk.server.config; import com.alibaba.druid.pool.DruidDataSource; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.data.repository.NoRepositoryBean; import javax.sql.DataSource; import java.sql.SQLException; @Configuration @PropertySource("classpath:database.properties") public class DatabaseConfig { @Value("${db.druid.driverClassName}") private String driverClassName ; @Value("${db.druid.url}") private String url ; @Value("${db.druid.username}") private String username ; @Value("${db.druid.password}") private String password ; @Value("${db.druid.maxActive}") private int maxActive ; @Value("${db.druid.minIdle}") private int minIdle ; @Value("${db.druid.initialSize}") private int initialSize ; @Value("${db.druid.maxWait}") private long maxWait ; @Value("${db.druid.timeBetweenEvictionRunsMillis}") private long timeBetweenEvictionRunsMillis ; @Value("${db.druid.minEvictableIdleTimeMillis}") private long minEvictableIdleTimeMillis ; @Value("${db.druid.validationQuery}") private String validationQuery ; @Value("${db.druid.testWhileIdle}") private boolean testWhileIdle ; @Value("${db.druid.testOnBorrow}") private boolean testOnBorrow ; @Value("${db.druid.testOnReturn}") private boolean testOnReturn ; @Value("${db.druid.poolPreparedStatements}") private boolean poolPreparedStatements ; @Value("${db.druid.maxPoolPreparedStatementPerConnectionSize}") private int maxPoolPreparedStatementPerConnectionSize ; @Value("${db.druid.filters}") private String filters ; @Bean("dataSource") public DataSource getDruidDataSource() { DruidDataSource dataSource = new DruidDataSource() ; dataSource.setDriverClassName(this.driverClassName); dataSource.setUrl(this.url); dataSource.setUsername(this.username); dataSource.setPassword(this.password); dataSource.setMaxActive(this.maxActive); dataSource.setMinIdle(this.minIdle); dataSource.setInitialSize(this.initialSize); dataSource.setMaxWait(this.maxWait); dataSource.setTimeBetweenEvictionRunsMillis(this.timeBetweenEvictionRunsMillis); dataSource.setMinEvictableIdleTimeMillis(this.minEvictableIdleTimeMillis); dataSource.setValidationQuery(this.validationQuery); dataSource.setTestWhileIdle(this.testWhileIdle); dataSource.setTestOnBorrow(this.testOnBorrow); dataSource.setTestOnReturn(this.testOnReturn); dataSource.setPoolPreparedStatements(this.poolPreparedStatements); dataSource.setMaxPoolPreparedStatementPerConnectionSize(this.maxPoolPreparedStatementPerConnectionSize); try { dataSource.setFilters(this.filters); } catch (SQLException e) { e.printStackTrace(); } return dataSource ; } }