做网站的股哥/运营seo是什么意思
springboot的搭建:http://blog.csdn.net/goligory/article/details/78404480
最近喜欢用springboot,有时间就研究了一下,因为经常用sqlserver,在网上查了半天没有什么很好的配置,在抽取业务层的时候也出点问题,还好解决了
这是一个比较简单的结构
先引入sqlserver和mysql的依赖,注意不要重复,这么低级的错误我都不知道自己怎么犯的
<!-- https://mvnrepository.com/artifact/com.microsoft.sqlserver/sqljdbc4 --><dependency><groupId>com.microsoft.sqlserver</groupId><artifactId>sqljdbc4</artifactId><version>4.0</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency>
这是sqlserver的连接配置
spring:datasource:url: jdbc:sqlserver://localhost:1433;DatabaseName=xxxxxdriver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriverusername: sapassword: 123456initial-size: 1min-idle: 1max-active: 20test-on-borrow: truemax-wait: 60000time-between-eviction-runs-millis: 60000min-evictable-idle-time-millis: 300000validation-query: SELECT 1 FROM DUALtest-While-Idle: truetest-on-return: falsepool-prepared-statements: falsemax-pool-prepared-statement-per-connection-size: 20filters: stat,wall,log4j,config
相关含义可以看看这篇文章:https://segmentfault.com/a/1190000004316491
initial-size:指定启动连接池时,初始建立的连接数量
min-idle:指定必须保持连接的最小值
max-active:指定连接池中最大的活跃连接数.
test-on-borrow:当从连接池借用连接时,是否测试该连接.
max-wait:指定连接池等待连接返回的最大等待时间,毫秒单位
time-between-eviction-runs-millis:指定空闲连接检查、废弃连接清理、空闲连接池大小调整之间的操作时间间隔
min-evictable-idle-time-millis:指定一个空闲连接最少空闲多久后可被清除.
validation-query:指定获取连接时连接校验的sql查询语句.
test-While-Idle当连接空闲时,是否执行连接测试.
test-on-return:在连接归还到连接池时是否测试该连接.
pool-prepared-statements:指定是否池化statements
其他的看demo
mysql连接配置,url是一行的我分开显示的
url: jdbc:mysql://localhost:3306/bdshop?createDatabaseIfNotExist=true&useUnicode=true&useUnicode=true&characterEncoding=utf8&autoReconnect=true&verifyServerCertificate=false&useSSL=false&sessionVariables=sql_mode='NO_ENGINE_SUBSTITUTION'&jdbcCompliantTruncation=falsedriver-class: com.mysql.jdbc.Driver
在访问数据库时指定编码方式是否使用userssl等等
抽取公共业务层:
这样重用率会好很多,业务层在一块,所有工具类在一块分类显示,这样整体结构不冗杂
下面细说一下抽取业务层:
BaseMapper<T>
public interface BaseMapper<T> {int insert(int id);
}
BaseService<T>:继承BaseMapper<T>
public interface BaseService<T> extends BaseMapper<T> { }
}
BaseServiceImp<T>实现BaseService<T>,定义一个返回mapper接口的抽象方法等会有用
public abstract class BaseServiceImp<T> implements BaseService<T> {protected abstract BaseMapper<T> getMapper();public int insert(int id){return getMapper().insert(id);}
}
下面看一下模块层mapper继承公共BaseMapper,传入请求参数
public interface AMapper extends BaseMapper<User> {public User login(User user);
}
service继承公共BaseService,传入请求参数,相当于继承了BaseMapper
public interface AService extends BaseService<User> {public User login(User user);
}
imp继承BaseServiceImp,实现模块Service,向公共层传入参数,注意加上@Service,公共可以不加
BaseServiceImp没有实现getMapper() 方法,在这里实现,return指定模块的aMapper,这样就相当于是aMapper调用insert了
这个地方我一开始没注意,调用insert时在BaseServiceImp能传入参数,但是请求不了BaseMapper去查询数据库,一直报null
@Transactional
@Service
public class AServiceImp extends BaseServiceImp<User> implements AService {@Resourceprivate AMapper aMapper;@Overrideprotected BaseMapper<User> getMapper(){return this.aMapper;}@Overridepublic User login(User user) {System.out.println("imp--"+user);user= aMapper.login(user);return user;}
}
AMapper.xml指定AMapper.java
这样controller不论调用login还是insert都可以进行查询了,清晰的结构还是蛮重要的
对于记录也是心得没多久,有不对地方多多指导,不好的地方多多包涵
码云地址:https://gitee.com/tickingbomb/tickingbomb/tree/mtgg/