通过Docker部署Java项目的日志输出
265 2023-04-03 04:28:27
对于其中的maven进行配置
在apache-maven-3.8.4\conf/settings.xml中添加:
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>central</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
<profile>
<id>JDK11</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>11</jdk>
</activation>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.compilerVersion>11</maven.compiler.compilerVersion>
</properties>
</profile>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.16</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.16</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.3.16</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<!-- 数据库连接池c3p0、驱动 -->
<!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.21</version>
</dependency>
(7) 其他包(jstl,servlet-api,junit)
<!-- https://mvnrepository.com/artifact/jstl/jstl -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.7</version>
</dependency>
<!-- SpringTest -->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.1.RELEASE</version>
</dependency>
在https://v3.bootcss.com/getting-started/#download中下载Bootstrap
在webapp中创建static文件夹存放下载bootstrap-3.4.1-dist的css、fonts、js和在js文件夹加入jquery-1.12.4.min.js文件
在index.jsp中head中添加
(1)<!-- 最新版本的 Bootstrap 核心 CSS 文件 --><link rel="stylesheet" href="static/bootstrap-3.4.1-dist/css/bootstrap.min.css"
integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">
(2)<!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
<script src="static/bootstrap-3.4.1-dist/js/bootstrap.min.js"
integrity="sha384-aJ21OjlMXNL5UyIl/XNwTMqvzeRMZH2w8c5cRVpzpU8Y5bApTppSuUkhZXN0VxHd" crossorigin="anonymous"></script>
(3)引入jquery
<script type="text/javascript" src="static/js/jquery-1.12.4.min.js"></script>
web.xml、spring、springmvc、mybatis,使用mybatis的逆向工程生成对应的bean以及mapper
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app id="WebApp_ID" version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"> 3 <!-- 1、启动Spring的容器 --> 4 <context-param> 5 <param-name>contextConfigLocation</param-name> 6 <param-value>classpath:applicationContext.xml</param-value> 7 </context-param> 8 9 <!-- 2、配置springmvc的前端控制器,拦截所有请求 -->10 <!-- 配置前端控制器DispatcherServlte,该类在spring-webmvc.jar包中,配置的目录是把用户的所有请求都教给springmvc框架来处理 -->11 <servlet>12 <servlet-name>DispatcherServlet</servlet-name>13 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>14 <!-- 当启动springmvc时,默认回到WEB-INF/目录下查找名称为DispatcherServlet-servlet.xml作为框架的核心配置文件进行加载 -->15 <!-- 为了提高springMVC框架的核心配置文件的灵活性,我们可以通过给该前端控制器配置初始化参数,来改变配置文件的位置即名称修改 -->16 <init-param>17 <!--contextConfigLocation初始化属性名称来指定Springmvc框架的核心配置文件及路径 -->18 <param-name>contextConfigLocation</param-name>19 <param-value>classpath:springmvc.xml</param-value>20 </init-param>21 </servlet>22 <servlet-mapping>23 <servlet-name>DispatcherServlet</servlet-name>24 <!-- 对外映射的访问路径"/"表示把所有的用户请求都交给springMVC框架来处理,主要不要写成"/*" -->25 <url-pattern>/</url-pattern>26 </servlet-mapping>27 <!-- 3、配置springmvc编码过滤器 --> 28 <!-- 为了解决乱码,我们可以配置编码的过滤器 ,该编码的过滤器在spring-web。jar中 -->29 <filter>30 <filter-name>CharacterEncodingFilter</filter-name>31 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>32 <!-- 配置相关的初始化参数,如采用的编码,请求及响应等属性 -->33 <init-param>34 <param-name>encoding</param-name><!-- 编码格式 -->35 <param-value>UTF-8</param-value>36 </init-param>37 <init-param>38 <param-name>forceRequestEncoding</param-name><!-- 请求编码 -->39 <param-value>true</param-value>40 </init-param>41 <init-param>42 <param-name>forceResponseEncoding</param-name><!-- 响应编码 -->43 <param-value>true</param-value>44 </init-param>45 46 </filter>47 <filter-mapping>48 <filter-name>CharacterEncodingFilter</filter-name>49 <url-pattern>/*</url-pattern>50 </filter-mapping>51 <welcome-file-list>52 <welcome-file>index.jsp</welcome-file>53 </welcome-file-list>54 <!-- 4配置普通请求转变 --> 55 <!-- 配置HiddenHttpMethodFilter 将普通post请求转换为指定的delete或者put请求-->56 <filter>57 <filter-name>hiddenHttpMethodFilter</filter-name>58 <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>59 </filter>60 <filter-mapping>61 <filter-name>hiddenHttpMethodFilter</filter-name>62 <url-pattern>/*</url-pattern>63 </filter-mapping>64 65 </web-app>
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:mvc="http://www.springframework.org/schema/mvc" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 8 http://www.springframework.org/schema/context 9 http://www.springframework.org/schema/context/spring-context-3.2.xsd 10 http://www.springframework.org/schema/mvc 11 http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">12 <!-- 1.配置开启基于注解的类扫描的注解解析器,主要针对#Controller,@Service,@Repository,@Component注解 -->13 <context:component-scan base-package="com.lijian"/>14 <!-- 2.配置开启基于注解方法的依赖的注解解析器配置,主要针对@Autowire,@Resource注解 -->15 <context:annotation-config/>16 <!-- 3.配置开启SpringMVC基于注解的处理器映射器和处理器适配器的解析器配置 -->17 <mvc:annotation-driven/>18 <!-- 4.配置视图解析器,目的是把逻辑视图解析为一个具体的物理视图,该类在spring-webmvc.jar包中 -->19 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">20 <!-- 通过配置属性来配置映射的规则 -->21 <!-- 配置前缀属性prefix -->22 <property name="prefix" value="/WEB-INF/views/"/>23 <!-- 配置后缀属性 -->24 <property name="suffix" value=".jsp"/>25 </bean>26 27 <!-- 需要配置自定义视图的解析器,该类名称为:org.springframework.web.servlet.view.BeanNameViewResolver,该类在springMVC.jar包中 -->28 <!-- 在一个应用中配置了多个视图解析器,其执行的顺序可以通过该视图解析器配置一个order属性来指定,默认order的值为整型的最大值,其值越小表示执行的优先级越高 -->29 <bean class="org.springframework.web.servlet.view.BeanNameViewResolver">30 <property name="order" value="100">31 32 </property>33 </bean>34 35 <mvc:view-controller path="/" view-name="index"></mvc:view-controller>36 <mvc:default-servlet-handler/>37 <!-- 能支持springmvc更高级一些的功能,JSR303校验,快捷ajax... 映射动态请求-->38 <!-- 开启mvc的注解驱动 -->39 <mvc:annotation-driven/>40 41 </beans>
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:aop="http://www.springframework.org/schema/aop" 5 xmlns:tx="http://www.springframework.org/schema/tx" 6 xmlns:context="http://www.springframework.org/schema/context" 7 xsi:schemaLocation=" 8 http://www.springframework.org/schema/beans 9 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 10 http://www.springframework.org/schema/tx 11 http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 12 http://www.springframework.org/schema/context 13 http://www.springframework.org/schema/context/spring-context-3.0.xsd 14 http://www.springframework.org/schema/aop 15 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> 16 <context:component-scan base-package="com.lijian">17 <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>18 </context:component-scan>19 <!-- Spring配置文件,这里主要配置和业务逻辑有关的 --> 20 <!-- ==================数据源,事务控制,xxx==================== --> 21 <context:property-placeholder location="classpath:dbconfig.properties"/>22 <bean id="pooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">23 <property name="jdbcUrl" value="${url}"></property>24 <property name="driverClass" value="${driver}"></property>25 <property name="user" value="${userame}"></property>26 <property name="password" value="${password}"></property>27 </bean> 28 29 <!--====================== 配置和mybatis整合 ===============-->30 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">31 <!-- 指定mybatis全局配置文件的位置 -->32 <property name="configLocation" value="classpath:mybatis-config.xml"></property>33 <property name="dataSource" ref="pooledDataSource"></property>34 <!-- 指定mybatis,mapper文件的位置 -->35 <property name="mapperLocations" value="classpath:mapper/*.xml"></property>36 </bean>37 <!-- 配置扫描器,将mybatis接口的实现加入到ioc容器 -->38 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">39 <!-- 扫描所有dao接口的实现,加入到ioc容器中 -->40 <property name="basePackage" value="com.lijian.crud.dao"></property>41 </bean>42 <!--================== 事务控制的配置======================-->43 44 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">45 <!-- 控制住数据源 -->46 <property name="datasource" ref="pooledDataSource"></property>47 </bean>48 49 <!-- 开启基于注解的事务,使用xml配置形式的事务(必要主要的都是使用配置式) -->50 <aop:config>51 <!-- 切入点表达式 -->52 <aop:pointcut expression="execution(* com.lijian.crud.service..*(..))" id="txPoint"/>53 <!-- 配置增强类 -->54 <aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/>55 </aop:config>56 <!-- 配置事务,事务如何切入 -->57 <tx:advice id="txAdvice">58 <tx:attributes>59 <!-- 所有方法都是事务方法 -->60 <tx:method name="*"/>61 <!-- 以get开始的所有方法 -->62 <tx:method name="get" read-only="true"/>63 </tx:attributes>64 </tx:advice>65 <!-- Spring配置文件的核心点(数据源,与mybatis的整合,事务控制) -->66 67 </beans>
https://mybatis.org/generator/running/runningWithJava.html查看开始生成bean和dao方法
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE generatorConfiguration 3 PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" 4 "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> 5 6 <generatorConfiguration> 7 8 <context id="DB2Tables" targetRuntime="MyBatis3"> 9 <!-- 配置数据库文件信息 -->10 <jdbcConnection11 driverClass="com.mysql.cj.jdbc.Driver"12 connectionURL="jdbc:mysql://localhost:3306/ssm_crud?characterEncoding=utf8&serverTimezone=UTC"13 userId="root"14 password="root"/>15 16 17 <javaTypeResolver >18 <property name="forceBigDecimals" value="false" />19 </javaTypeResolver>20 <!--指定javaBean生成位置 -->21 <javaModelGenerator targetPackage="com.lijian.crud.bean" 22 targetProject=".\src\main\java">23 <property name="enableSubPackages" value="true" />24 <property name="trimStrings" value="true" />25 </javaModelGenerator>26 <!-- 指定sql映射文件生成的位置 -->27 <sqlMapGenerator targetPackage="mapper" 28 targetProject=".\src\main\resources">29 <property name="enableSubPackages" value="true" />30 </sqlMapGenerator>31 <!-- 指定dao接口生成位置,mapper接口 -->32 <javaClientGenerator type="XMLMAPPER" targetPackage="com.lijian.crud.dao" 33 targetProject=".\src\main\java">34 <property name="enableSubPackages" value="true" />35 </javaClientGenerator>36 <!-- table指定每个表的生成策略 -->37 <table schema="DB2ADMIN" tableName="tbl_emp" domainObjectName="Employee" />38 <table schema="DB2ADMIN" tableName="tbl_dept" domainObjectName="Department" />39 </context>40 </generatorConfiguration>
在配置<!-- 配置数据库文件信息 -->时
connectionURL="jdbc:mysql://localhost:3306/ssm_crud?characterEncoding=utf8&serverTimezone=UTC"
需要加上时区信息,并用&连接,这是xml配置信息,所有得按照这种格式
1 public static void main(String[] args) throws Exception { 2 List<String> warnings = new ArrayList<String>(); 3 boolean overwrite = true; 4 File configFile = new File("mbg.xml"); 5 ConfigurationParser cp = new ConfigurationParser(warnings); 6 Configuration config = cp.parseConfiguration(configFile); 7 DefaultShellCallback callback = new DefaultShellCallback(overwrite); 8 MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings); 9 myBatisGenerator.generate(null);10 }
执行之后,会根据配置信息,在对应位置自动生成实体类与其对应的dao接口
可向mbg.xml中的<context>中前面添加
<commentGenerator> <property name="suppressDate" value="true" /></commentGenerator>
取消生成注释属性
工程文件分布如图:
在employee表中添加
private Department department;
以及setter、getter方法
在EmployeeMapper.java中添加
List<Employee> selectByExampleWithDept(EmployeeExample example);
Employee selectByPrimaryKeyWithDept(Integer empId);
在EmployeeMapper.xml中添加对应是sql实现语句
1 <!-- 带部门信息的结果集合 --> 2 <resultMap id="WithDeptResultMap" 3 type="com.lijian.crud.bean.Employee" > 4 <id column="emp_id" jdbcType="INTEGER" property="empId" /> 5 <result column="emp_name" jdbcType="VARCHAR" property="empName" /> 6 <result column="gender" jdbcType="CHAR" property="gender" /> 7 <result column="email" jdbcType="VARCHAR" property="email" /> 8 <result column="d_id" jdbcType="INTEGER" property="dId" /> 9 <!-- 指定联合查询出部门字段的封装 -->10 <association property="department" javaType="com.lijian.crud.bean.Department">11 <id column="dept_id" property="deptId"/>12 <result column="dept_name" property="deptName"/>13 </association>14 </resultMap>
1 <select id="selectByExampleWithDept"> 2 select 3 <if test="distinct"> 4 distinct 5 </if> 6 <include refid="Base_Column_List" /> 7 from tbl_emp 8 left join tbl_dept on tbl_emp.d_id=tbl_dept.dept_id 9 <if test="_parameter != null">10 <include refid="Example_Where_Clause" />11 </if>12 <if test="orderByClause != null">13 order by ${orderByClause}14 </if>15 </select>
1 <select id="selectByPrimaryKeyWithDept">2 parameterType="java.lang.Integer" resultMap="BaseResultMap">3 select4 <include refid="WithDept_Column_list" />5 from tbl_emp6 left join tbl_dept on tbl_emp.d_id=tbl_dept.dept_id7 where emp_id = #{empId,jdbcType=INTEGER}8 9 </select>
在com.lijian.crud.controller中添加测试类,测试是否配置正常,生成的逆向工程是否能运行
1 package com.lijian.crud.test; 2 import org.junit.Test; 3 import org.junit.runner.RunWith; 4 import org.springframework.beans.factory.annotation.Autowired; 5 import org.springframework.test.context.ContextConfiguration; 6 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 7 8 import com.lijian.crud.dao.DepartmentMapper; 9 import com.lijian.crud.dao.EmployeeMapper;10 @RunWith(SpringJUnit4ClassRunner.class)11 @ContextConfiguration(locations ="classpath:applicationContext.xml")12 public class MapperTest {13 /**14 * 测试15 * 推荐String项目就可以使用Spring单元测试,可以自动注入我们需要的组件16 *1. 导入SpringTest模块17 *2.@ContextConfiguration指定spring配置文件的位置18 *3.直接Autowired19 */20 @Autowired21 DepartmentMapper departmentMapper;22 @Autowired23 EmployeeMapper employeeMapper;24 @Test25 public void testCRUD() {26 //1.创建SpringIOC容器27 //ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");28 //2.从容器中获取mapper29 //ioc.getBean(DepartmentMapper.class);30 System.out.println(departmentMapper);31 System.out.println(employeeMapper);32 }33 34 }