一、添加pom.xml
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1</version> </dependency> <dependency><!-- 添加该jar后springboot默认启用连接池 --> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!-- 连接池配置 START --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.20</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.4</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> </dependency> <!-- 连接池配置 END --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.1</version> </dependency> <!-- 分页插件 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.1.2</version> </dependency> <!-- alibaba的druid数据库连接池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.0</version> </dependency>
二、应用程序作为客户端注册
@SpringBootApplication @EnableEurekaClient --开启作为客户端注册 @MapperScan("com.shiro.dao") --扫描包下的接口 public class ShiroServer { public static void main(String[] args) { new SpringApplicationBuilder(ShiroServer.class).web(true).run(args); } }
三、配置applicaton.yml
spring: datasource: type: com.alibaba.druid.pool.DruidDataSource url: jdbc:mysql://localhost:3306/数据库名?useSSL=false&useUnicode=true&characterEncoding=utf-8 username: root password: wubiaowp driver-class-name: com.mysql.jdbc.Driver initialSize: 5 maxActive: 200 minIdle: 5 maxWait: 60000 timeBetweenEvictionRunsMillis: 60000 minEvictableIdleTimeMillis: 300000 validationQuery: select 'x' testWhileIdle: true testOnBorrow: false testOnReturn: false poolPreparedStatements: true maxOpenPreparedStatements: 20 mybatis: mapper-locations: classpath:mapper/*_mapper.xml type-aliases-package: com.util.model #pagehelper分页插件可选 pagehelper: helperDialect: mysql reasonable: true supportMethodsArguments: true params: count=countSql
四、配置bootstrap.yml
server: port: 9012 spring: application: name: springCloud-shiro eureka: instance: hostname: localhost client: register-with-eureka: true fetch-registry: false service-url: defaultZone: http://localhost:9001/eureka/
五、编写mapper.xml文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//com.order.test.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.shiro.dao.UserMapper"> <resultMap id="userMap" type="com.util.model.User"> <id property="id" column="id"/> <result property="username" column="username"/> <result property="password" column="password"/> <result property="salt" column="salt"/> <result property="state" column="state"/> </resultMap> <select id="findByUserName" parameterType="string" resultMap="userMap"> .... </select> </mapper>
六、Mapper接口
@Repository public interface UserMapper { User findByUserName(String username); }
七、注入service层调用
@Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public User findByUserName(String username) { return userMapper.findByUserName(username); } }