Springboot-JDBC&Druid&一些Web组件

配置文件

  1. pom.xml
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.13</version>
    <scope>runtime</scope>
</dependency>
  1. application.yml
spring:
  datasource:
    username: root
    password: 932567
    url: jdbc:mysql://192.168.199.131:3307/123?useSSL=FALSE&&serverTimezone=GMT&&characterEncoding=utf-8
    driver-class-name: com.mysql.cj.jdbc.Driver
    # 选择Datasource类型
    type: com.alibaba.druid.pool.DruidDataSource
  1. Controller,简单地增删改查
@Autowired //已根据数据源自动注入
JdbcTemplate jdbcTemplate;

//简单的增删改查
@RequestMapping("/customerList")
public List<Map<String,Object>> userList(){
    String sql = "select * from cust_customer";
    List<Map<String, Object>> list_map = jdbcTemplate.queryForList(sql);
    return list_map;
}

@RequestMapping("/addPerson")
public String addUser(){
    String sql = "insert into person values('Acai',16)";
    jdbcTemplate.update(sql);
    System.out.println(jdbcTemplate.getDataSource().getClass());
    return "update_ok";
}

@RequestMapping("/updatePerson")
public String updateUser(){
    String sql = "update person set name = ? where age = ? ";
    //封装
    Object[] objects = new Object[2];
    objects[0] = "张三";
    objects[1] = 16;
    jdbcTemplate.update(sql,objects);
    return "update_ok";
}

@RequestMapping("/deletePerson/{age}")
public String updateUser( @PathVariable int age){
    String sql = "delete from person where age = ?";
    jdbcTemplate.update(sql,age);
    return "delete_ok";
}

添加一些组件

Druid

  1. pom.xml
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.21</version>
</dependency>

<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>
  1. config包下DruidConfig
@Configuration
public class DruidConfig {
    @ConfigurationProperties(prefix = "spring.datasource")
    @Bean
    public DataSource druidDataSource(){
        return new DruidDataSource();
    }

    //后台监控 通过/druid访问
    //因为SpringBoot内置了servlet容器,所以没有web.xml,替代方法:ServletRegistrationBean
    @Bean
    public ServletRegistrationBean statViewServlet(){
        //固定写法
        ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");
        //后台需要有人登陆,账号密码配置
        HashMap<String, String> map = new HashMap<>();

        //增加配置
        map.put("loginUsername","admin");//loginUsername是固定的写法
        map.put("loginPassword","1");//loginPassword是固定的写法

        //允许谁可以访问
        map.put("allow","");//allow固定写法,value是主机地址,空值表示谁都可以

        //禁止谁可以访问
        //map.put("自定义名字","192.168.199.131");

        bean.setInitParameters(map); //设置初始化参数
        return bean;
    }

其他一些web组件

//filter
@Bean
public FilterRegistrationBean webStatFilter(){
    FilterRegistrationBean bean = new FilterRegistrationBean();
    bean.setFilter(new WebStatFilter());

    //过滤那些请求,从源码可得
    /*"profileEnable";
    "sessionStatEnable";
    "sessionStatMaxCount";
    "exclusions";
    "principalSessionName"
    "principalCookieName";
    "realIpHeader";*/
    HashMap<String, String> map = new HashMap<>();
    //这些东西不进行过滤
    map.put("exclusions","*.js,*.css,/druid/*");
    return bean;
}
Spring Boot和MyBatis-Plus是现代Java应用开发中的两个流行框架组合,它们一起简化了企业级应用程序的快速搭建。以下是一个基本的Spring Boot + MyBatis-Plus的整体开发流程: 1. **项目初始化**: - 创建一个新的Spring Boot项目(使用Maven或Gradle构建工具)。 - 添加Spring Boot Web、Spring Data JPA和MyBatis-Plus的依赖。 2. **数据库配置**: - 配置数据源(如HikariCP, Druid, 或者Spring Boot内置的DataSource)。 - 定义数据库连接池和JDBC驱动。 3. **实体类映射**: - 创建Java实体类(Entity),它们将对应数据库表结构。 - 使用MyBatis-Plus的Model Generator工具自动生成基础CRUD操作的代码。 4. **Mapper接口生成**: - 通过MyBatis-Plus的全局配置文件(GlobalConfig),指定Mapper接口生成的位置。 5. **Service层编写**: - 实现业务逻辑的服务类(Service),这些类通常会依赖于MyBatis-Plus的Mapper接口进行数据库操作。 6. **Repository接口和实现**: - 如果需要,可以创建Repository接口,继承自MyBatis-Plus的BaseMapper,进一步定制化查询。 7. **Controller层处理**: - 创建RESTful API控制器(Controller),调用Service层的方法,并返回JSON响应给客户端。 8. **配置数据初始化**: - 编写Spring Boot的启动类(Application)中的main方法,可能包含一些数据初始化操作。 9. **测试**: - 使用单元测试(JUnit, TestNG等)确保各个组件正常工作,如Service和Mapper接口的测试。 10. **部署**: - 部署项目到生产环境,如Tomcat, Jetty, 或者云服务器上的Web容器。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值