Spring学习-06 Spring+SpringMVC+Druid+jdbcTemplate(xml方式)

Spring学习-06 Spring+SpringMVC+Druid+jdbcTemplate(xml方式)

注意: 是在04(Spring+SpringMVC)完成后的的基础上操作,

06.01 导入依赖(pom.xml)

  • mysql数据库依赖
  • jdbctTemplate依赖
  • druid依赖
<dependency>
    <groupId>mysqlgroupId>
    <artifactId>mysql-connector-javaartifactId>
dependency>
<dependency>
    <groupId>org.springframeworkgroupId>
    <artifactId>spring-jdbcartifactId>
dependency>
<dependency>
    <groupId>com.alibabagroupId>
    <artifactId>druidartifactId>
dependency>

06.02 增加数据库配置文件(jdbc.properties)

# JDBC
# MySQL 8.x: com.mysql.cj.jdbc.Driver
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.connectionURL=jdbc:mysql://127.0.0.1:3306/2019springtest?useUnicode=true&characterEncoding=utf-8&useSSL=false
jdbc.username=root
jdbc.password=root

# JDBC Pool
jdbc.pool.init=1
jdbc.pool.minIdle=3
jdbc.pool.maxActive=20

# JDBC Test
jdbc.testSql=SELECT 'x' FROM DUAL

06.03 druid配置文件(resources\spring-context-druid.xml)

  • 配置datasource
    • 数据源配置com.alibaba.druid.pool.DruidDataSource
    • 配置监控统计拦截的filters,不写的话无法监控sql执行
  • 配置jdbcTemplate

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    
    <context:property-placeholder ignore-unresolvable="true"
                                  location="classpath:jdbc.properties"/>
    
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
          init-method="init" destroy-method="close">
        
        <property name="driverClassName" value="${jdbc.driverClass}"/>

        
        <property name="url" value="${jdbc.connectionURL}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>

        
        <property name="initialSize" value="${jdbc.pool.init}"/>
        <property name="minIdle" value="${jdbc.pool.minIdle}"/>
        <property name="maxActive" value="${jdbc.pool.maxActive}"/>

        
        <property name="maxWait" value="60000"/>

        
        <property name="timeBetweenEvictionRunsMillis" value="60000"/>

        
        <property name="minEvictableIdleTimeMillis" value="300000"/>

        <property name="validationQuery" value="${jdbc.testSql}"/>
        <property name="testWhileIdle" value="true"/>
        <property name="testOnBorrow" value="false"/>
        <property name="testOnReturn" value="false"/>

        
        <property name="filters" value="stat"/>
    bean>

    
    <bean id="jdbcTemplate" 
          class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource" />
    bean>
beans>

06.04 配置web.xml

  • StatViewServlet配置监控中心,指定登录用户密码(可选),指定登录路径(/druid/*)
  • WebStatFilter排除不监控的对象
   
    <servlet>
        <servlet-name>DruidStatViewservlet-name>
        <servlet-class>com.alibaba.druid.support.http.StatViewServletservlet-class>
        <init-param>
            <param-name>loginUsernameparam-name>
            <param-value>adminparam-value>
        init-param>
        <init-param>
            <param-name>loginPasswordparam-name>
            <param-value>adminparam-value>
        init-param>
    servlet>
    <servlet-mapping>
        <servlet-name>DruidStatViewservlet-name>
        <url-pattern>/druid/*url-pattern>
    servlet-mapping>

    <filter>
        <filter-name>DruidWebStatFilterfilter-name>
        <filter-class>com.alibaba.druid.support.http.WebStatFilterfilter-class>
        <init-param>
            <param-name>exclusionsparam-name>
            <param-value>*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*param-value>
        init-param>
    filter>
    <filter-mapping>
        <filter-name>DruidWebStatFilterfilter-name>
        <url-pattern>/*url-pattern>
    filter-mapping>

06.05 测试用Entity(User.java)

  • 使用的lombok支持
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
     
    private Integer id;
    private String username;
    private String password;
    private String avatar;
    private Date created;
    private Date updated;
}

06.06 测试用DAO(UserDao.java)

  • 创建方法finaAll取得所有的User数据
  • 需要实现一个UserRowMapper,用来做映射数据
@Repository
public class UserDao {
     
    @Autowired
    JdbcTemplate jdbcTemplate;
    
    public List<User> findAll() {
     
        return jdbcTemplate.query("select * from user", new UserRowMapper());

    }
    class UserRowMapper implements RowMapper<User> {
     
        @Override
        public User mapRow(ResultSet rs, int i) throws SQLException {
     
            User user = new User();
            user.setId(rs.getInt("id"));
            user.setUsername(rs.getString("username"));
            user.setPassword(rs.getString("password"));
            user.setAvatar(rs.getString("avatar"));
            user.setCreated(rs.getDate("created"));
            user.setUpdated(rs.getDate("updated"));
            return user;
        }
    }
}

06.07 测试用Controller(UserController.java)

  • 注入UserDao
  • 创建接口/user/list,返回前台JSON数据
@Controller
public class UserController {
     

    @Autowired
    private UserDao userDao;

    @RequestMapping("/user/list")
    @ResponseBody
    public List<User> findall() {
     
        return userDao.findAll();
    }
}
``

你可能感兴趣的:(spring,java,spring)