1.导入依赖
<!-- springboot,mybatis 整合包 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!-- springboot,druid 整合包 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<!--web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--tomcat-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<!--test-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
2.配置application.properties
#数据连接
spring.datasource.druid.driverClassName=com.mysql.jdbc.Driver
spring.datasource.druid.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
spring.datasource.druid.username=root
spring.datasource.druid.password=root123
spring.datasource.druid.initialSize=5
spring.datasource.druid.minIdle=5
spring.datasource.druid.maxActive=20
spring.datasource.druid.maxWait=60000
spring.datasource.druid.timeBetweenEvictionRunsMillis=60000
spring.datasource.druid.min-evictableIdleTimeMillis=300000
spring.datasource.druid.validationQuery=SELECT 1 FROM DUAL
spring.datasource.druid.testWhileIdle=true
spring.datasource.druid.testOnBorrow=false
spring.datasource.druid.testOnReturn=false
spring.datasource.druid.poolPreparedStatements=true
spring.datasource.druid.maxPoolPreparedStatementPerConnectionSize=20
spring.datasource.druid.filters=stat,wall
# mybatis 配置
mybatis.config-location=classpath:mybatis/mybatis-config.xml
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
3.编程
@Configuration
public class DruidConfiguration {
//引用application.yml中的配置
@ConfigurationProperties(prefix = "spring.datasource.druid")
@Bean
public DataSource druid(){
return new DruidDataSource();
}
/**
* 配置Druid的监控
* 1、配置一个管理后台的Servlet
* @return
*/
@Bean
public ServletRegistrationBean statViewServlet(){
ServletRegistrationBean bean=new ServletRegistrationBean(new StatViewServlet(),"/druid/*");
Map<String,String> initParams=new HashMap<>();
//白名单:
initParams.put("allow","");//默认就是允许所有访问
//IP黑名单 (存在共同时,deny优先于allow) : 如果满足deny的话提示:Sorry, you are not permitted to view this page.
initParams.put("deny","192.20.10.5");
//Druid监控后台登录账号密码
initParams.put("loginUsername","admin");
initParams.put("loginPassword","123456");
bean.setInitParameters(initParams);
return bean;
}
/**
* 2、配置一个web监控的filter
* @return
*/
@Bean
public FilterRegistrationBean webStatFilter() {
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());
//添加过滤规则.
filterRegistrationBean.addUrlPatterns("/*");
//添加不需要忽略的格式信息.
filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid2/*");
return filterRegistrationBean;
}
}
4.创建实体类
public class User {
private Integer uId;
private String uName;
}
此处省略setter和getter
5.创建接口
public interface UserDao {
int insertUser(User user);
User getUserById(Integer uid);
int updateUser(User user);
int deleteUserById(Integer uid);
}
6.添加配置文件
6.1 创建mybatis-config.xml (resources\mybatis 文件夹自己创)
<?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="useGeneratedKeys" value="true"/>
<!-- 使用列别名替换列名,默认为 true -->
<setting name="useColumnLabel" value="true"/>
<!-- 开启驼峰命名转换:Table(create_time) => Entity(createTime) -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
</configuration>
6.2 创建UserMapper.xml (resources\mybatis\mapper 文件夹自己创)
<?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="club.lygangdai.dao.UserDao">
<insert id="insertUser" parameterType="club.lygangdai.pojo.User">
insert into t_user(u_name) values(#{uName})
</insert>
<select id="getUserById" parameterType="java.lang.Integer" resultType="club.lygangdai.pojo.User">
select u_id,u_name from t_user where u_id = #{uId}
</select>
<update id="updateUser" parameterType="club.lygangdai.pojo.User">
update t_user set u_name = #{uName} where u_id = #{uId}
</update>
<delete id="deleteById" parameterType="java.lang.Integer">
delete from t_user where u_id = #{uId}
</delete>
</mapper>
注意:在启动类添加注解 @MapperScan(dao包路径)
7.测试
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootDruidApplicationTests {
@Autowired
private UserDao userDao;
@Test
public void contextLoads() {
User user = userDao.getUserById(1);
System.out.println(user);
}
}
结果图如下:
在浏览器输入:http://127.0.0.1:8080/druid/login.html
进监听登录
根据配置的账户密码登录
评论 (0)