javaweb 学习记录(一)

时至今日,工作了一年多,感觉自己好像什么都不会,又都听过一点。

总觉得不能这么下去,于是决定做点东西,边做边学吧,实践是最好的老师嘛。

此文仅用于记录学习过程中,一些我觉得我该记住的东西。

一开始因为想学习多点东西,决定使用springboot搭一个项目,根据网络教程搭建完毕后,pom.xml文件如下,自己有所更改:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>

   <groupId>com.avivi</groupId>
   <artifactId>mercenary</artifactId>
   <version>0.0.1</version>
   <packaging>jar</packaging>

   <name>mercenary</name>
   <description>mercenary</description>

   <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>2.0.4.RELEASE</version>
      <relativePath/> <!-- lookup parent from repository -->
   </parent>

   <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
      <java.version>1.8</java.version>
   </properties>

   <dependencies>
      <!--springboot-->
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-jdbc</artifactId>
      </dependency>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-test</artifactId>
         <scope>test</scope>
      </dependency>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-websocket</artifactId>
      </dependency>
      <!-- servlet依赖 -->
      <dependency>
         <groupId>javax.servlet</groupId>
         <artifactId>javax.servlet-api</artifactId>
         <scope>provided</scope>
      </dependency>
      <dependency>
         <groupId>javax.servlet</groupId>
         <artifactId>jstl</artifactId>
      </dependency>
      <!-- tomcat 的支持 -->
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-tomcat</artifactId>
         <scope>provided</scope>
      </dependency>
      <dependency>
         <groupId>org.apache.tomcat.embed</groupId>
         <artifactId>tomcat-embed-jasper</artifactId>
      </dependency>
      <!--工具类-->
      <dependency>
         <groupId>org.apache.commons</groupId>
         <artifactId>commons-lang3</artifactId>
         <version>3.4</version>
      </dependency>
      <!-- 分页插件 -->
      <dependency>
         <groupId>com.github.pagehelper</groupId>
         <artifactId>pagehelper-spring-boot-starter</artifactId>
         <version>1.2.5</version>
      </dependency>
      <!-- alibaba的druid数据库连接池 -->
      <dependency>
         <groupId>com.alibaba</groupId>
         <artifactId>druid-spring-boot-starter</artifactId>
         <version>1.1.9</version>
      </dependency>
      <!--mysql-->
      <dependency>
         <groupId>mysql</groupId>
         <artifactId>mysql-connector-java</artifactId>
         <scope>runtime</scope>
      </dependency>
      <!--json-->
      <dependency>
         <groupId>com.alibaba</groupId>
         <artifactId>fastjson</artifactId>
         <version>1.2.46</version>
      </dependency>
      <!--热部署-->
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-devtools</artifactId>
         <optional>true</optional>
      </dependency>
   </dependencies>

   <build>
      <plugins>
         <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
         </plugin>
      </plugins>
   </build>
</project>

application.yml文件如下:

server:
  port: 8080

spring:
    mvc: 
        view:
          prefix: /
          suffix: .jsp
    datasource:
        name: mercenary
        type: com.alibaba.druid.pool.DruidDataSource
        #druid相关配置
        druid:
          #监控统计拦截的filters
          filters: stat
          driver-class-name: com.mysql.jdbc.Driver
          #基本属性
          url: jdbc:mysql://127.0.0.1:3306/mercenary?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false
          username: root
          password: 123456
          #配置初始化大小/最小/最大
          initial-size: 1
          min-idle: 1
          max-active: 20
          #获取连接等待超时时间
          max-wait: 60000
          #间隔多久进行一次检测,检测需要关闭的空闲连接
          time-between-eviction-runs-millis: 60000
          #一个连接在池中最小生存的时间
          min-evictable-idle-time-millis: 300000
          validation-query: SELECT 'x'
          test-while-idle: true
          test-on-borrow: false
          test-on-return: false
          #打开PSCache,并指定每个连接上PSCache的大小。oracle设为true,mysql设为false。分库分表较多推荐设置为false
          pool-prepared-statements: false
          max-pool-prepared-statement-per-connection-size: 20
    
## 该配置节点为独立的节点,有很多同学容易将这个配置放在spring的节点下,导致配置无法被识别
mybatis:
  mapper-locations: classpath:mapper/*.xml  #注意:一定要对应mapper映射xml文件的所在路径
  type-aliases-package: com.avivi.mercenary.model  # 注意:对应实体类的路径

#控制台输出sql
logging:
  level:
    com.avivi.mercenary.dao: debug

#pagehelper
pagehelper:
    helperDialect: mysql
    reasonable: true
    supportMethodsArguments: true
    params: count=countSql
    returnPageInfo: check

根据网络,似乎属性:type-aliases-package可以在mybatis xml文件中使用实体类别名,有效

但idea编译器对于别名报红,个人强迫症原因,我引用了mybatis的配置文件mybatis-config.xml如下:

<?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>
      <!--设置启用数据库字段下划线映射到java对象的驼峰式命名属性,默认为false-->
      <setting name="mapUnderscoreToCamelCase" value="true"/>
   </settings>
   
   <!--实体类别名-->
   <typeAliases>
      <typeAlias type="com.avivi.mercenary.model.UserInfo" alias="userInfo" />
   </typeAliases>
   
</configuration>

引用方式:

@ImportResource(locations={"classpath:mybatis-config.xml"}) //使用本地mybatis配置,实体类

在springboot启动类添加注解

其中属性mapUnderscoreToCamelCase,可启用数据库字段下划线映射到java对象的驼峰式命名

然而我项目中不知为何无效。最后还是使用了resultmap实现映射。(问题记录,留待解决)

注:有些注释来源于网络

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页