修改pom.xml文件,整合mybatis须要以下的包css
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <!--mybatis的starter--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.0.0</version> </dependency> <!--mysql驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <!--druid链接池--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.12</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.10</version> </dependency>
spring: datasource: username: root password: root url: jdbc:mysql://127.0.0.1:3306/sff_test driver-class-name: com.mysql.jdbc.Driver # 指定本身使用的数据源 type: com.alibaba.druid.pool.DruidDataSource # DruidDataSource 其余属性配置 druid: initialSize: 5 minIdle: 5 maxActive: 20 maxWait: 60000 timeBetweenEvictionRunsMillis: 60000 minEvictableIdleTimeMillis: 300000 validationQuery: SELECT 1 FROM DUAL testWhileIdle: true testOnBorrow: false testOnReturn: false poolPreparedStatements: true maxPoolPreparedStatementPerConnectionSize: 20 useGlobalDataSourceStat: true filter: stat: enabled: true log-slow-sql: true wall: enabled: true
@Configurable public class DruidConfig { @Bean @ConfigurationProperties("spring.datasource.druid") public DataSource dataSourceTwo() { return DruidDataSourceBuilder.create().build(); } /** * 配置一个管理后台的Servlet * * @return */ @Bean public ServletRegistrationBean statViewServlet() { ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*"); Map<String, String> initParams = new HashMap<>(); initParams.put("loginUsername", "admin"); initParams.put("loginPassword", "admin"); initParams.put("allow", "");//默认就是容许全部访问 bean.setInitParameters(initParams); return bean; } /** * 配置一个web监控的filter * * @return */ @Bean public FilterRegistrationBean webStatFilter() { FilterRegistrationBean bean = new FilterRegistrationBean(); bean.setFilter(new WebStatFilter()); Map<String, String> initParams = new HashMap<>(); //经过 localhost:8080/druid/ 能够访问到监控后台 initParams.put("exclusions", "*.js,*.css,/druid/*"); bean.setInitParameters(initParams); bean.setUrlPatterns(Arrays.asList("/*")); return bean; } }
@Mapper public interface DeptMapper { @Select("select * from dept") List<Dept> selectAll(); }
public class MybatisConfig { //定制mybatis配置 @Bean public ConfigurationCustomizer configurationCustomizer() { return new ConfigurationCustomizer() { @Override public void customize(Configuration configuration) { //开启驼峰命令 configuration.setMapUnderscoreToCamelCase(true); } }; } }
dept_name
等字段没法转换为bean中的deptName
字段mapper
接口不少的时候,好比说有多个相似 DeptMapper
时,若是每一个mapper
都写@Mapper
注解很麻烦,此时能够在MybatisConfig
配置类上使用@MapperScan(value = "com.sff.spring.boot.mybatis.dao")
这个注解,去掉@Mapper
注解便可public interface EmpMapper { List<Emp> selectAll(); }
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.sff.spring.boot.mybatis.dao.EmpMapper"> <select id="selectAll" resultType="Emp"> select * from emp </select> </mapper>
注意:使用mapper的xml文件时,记得将mapper接口注入的容器中,使用@Mapper
`或者@MapperScan
注解 html
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <settings> <!--开启驼峰命名--> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings> <typeAliases> <!--实体bean路径--> <package name="com.sff.spring.boot.mybatis.domain"/> </typeAliases> </configuration>
mybatis: mapper-locations: classpath:mybatis/mapper/*.xml config-location: classpath:mybatis/mybatis-config.xml