文章目录
结构图
导入mybatis-jar包
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.21</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-bom</artifactId>
<version>2.13.3</version>
</dependency>
maven:静态资源过滤
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
配置druid数据源,过滤器
@Configuration
public class DruidConfig {
//配置数据源
@ConfigurationProperties(prefix = "spring.datasource")
@Bean
public DataSource druidDataSource() {
return new DruidDataSource();
}
//durid数据源监控
public ServletRegistrationBean statViewServlet() {
ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");
HashMap<String, String> map = new HashMap<>();
//后台登陆账号密码
map.put("loginUsername", "admin");
map.put("loginPassword", "123456");
//后台谁可以访问
map.put("allow", "");
//设置初始化参数,map必须是<string,string>,源码中规定的
bean.setInitParameters(map);
return bean;
}
//配置过滤器bean,反正都是xxxRefistrationBean
public FilterRegistrationBean webStatFilter() {
FilterRegistrationBean<WebStatFilter> bean = new FilterRegistrationBean<>(new WebStatFilter());
HashMap<String, String> map = new HashMap<>();
map.put("extensions", "*.js,*.css");
bean.setInitParameters(map);
return bean;
}
pojo,mapper,mapper.xml
public class Pojo implements Serializable {
//编号
//用户名
//密码
private Long id;
private String username;
private String password;
//省略get set方法
@Mapper
@Repository
public interface PojoMapper {
List<Pojo> findAll();
}
<mapper namespace="com.ncp.mapper.PojoMapper">
<select id="findAll" resultType="pojo">
select *
from department;
</select>
</mapper>
service,controller
public interface PojoService {
List<Pojo> findAll();
}
@Service
public class PojoServiceImpl implements PojoService {
@Override
public List<Pojo> findAll() {
return null;
}
}
@Controller
@RequestMapping("/pojo")
public class PojoController {
@RequestMapping("/select")
public String select() {
return "select";
}
}
application.yaml
spring:
datasource:
username: root
password: 123456
url: jdbc:mysql://localhost:3306/
driver-class-name: com.mysql.cj.jdbc.Driver
#数据源是阿里巴巴的druid
type: com.alibaba.druid.pool.DruidDataSource
mybatis:
#mybatis中的type-aliases-package是为了配置xml文件中resultType返回值的包位置
type-aliases-package: com.ncp.pojo
#mybatis中的mapper-locations是mapper的xml文件位置
mapper-locations: classpath:mapper/*.xml
#开启驼峰命名
configuration:
map-underscore-to-camel-case: true
spring启动类
@SpringBootApplication
@MapperScan("com/ncp/mapper")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}