前言
在使用 InelliJ IDEA 的过程中,如果能熟练的配置常用模块应用于实际开发过程中,效率将大大提高!!! Try it !
提示:以下是本篇文章正文内容
第一步:File —>Settings
第二步:
mysql.properties
注:3306/数据库名?useSSL=false根据自己数据库名修改即可
driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/2010?useSSL=false username=root password=root
oracle.properties
注:账户和密码根据实际情况修改
driver=oracle.jdbc.OracleDriver url=jdbc:oracle:thin:@localhost:1521:xe username=root password=root
其他配置文件也如此
log4j.properties
# # Copyright 2009-2021 the original author or authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # log4j.rootLogger=DEBUG,CONSOLE,FILE,stdout log4j.addivity.org.apache=true # 应用于控制台 log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender log4j.appender.CONSOLE.Threshold=INFO log4j.appender.CONSOLE.Target=System.out log4j.appender.CONSOLE.Encoding=UTF-8 log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout log4j.appender.CONSOLE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n # 每天新建日志 log4j.appender.A1=org.apache.log4j.DailyRollingFileAppender log4j.appender.A1.File=D:/log4j/log log4j.appender.A1.Encoding=UTF-8 log4j.appender.A1.Threshold=DEBUG log4j.appender.A1.DatePattern='.'yyyy-MM-dd log4j.appender.A1.layout=org.apache.log4j.PatternLayout log4j.appender.A1.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L : %m%n #应用于文件 log4j.appender.FILE=org.apache.log4j.FileAppender log4j.appender.FILE.File=C:/log4j/file.log log4j.appender.FILE.Append=false log4j.appender.FILE.Encoding=UTF-8 log4j.appender.FILE.layout=org.apache.log4j.PatternLayout log4j.appender.FILE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n # 应用于文件回滚 log4j.appender.ROLLING_FILE=org.apache.log4j.RollingFileAppender log4j.appender.ROLLING_FILE.Threshold=ERROR log4j.appender.ROLLING_FILE.File=rolling.log log4j.appender.ROLLING_FILE.Append=true log4j.appender.CONSOLE_FILE.Encoding=UTF-8 log4j.appender.ROLLING_FILE.MaxFileSize=10KB log4j.appender.ROLLING_FILE.MaxBackupIndex=1 log4j.appender.ROLLING_FILE.layout=org.apache.log4j.PatternLayout log4j.appender.ROLLING_FILE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n #自定义Appender log4j.appender.im = net.cybercorlin.util.logger.appender.IMAppender log4j.appender.im.host = mail.cybercorlin.net log4j.appender.im.username = username log4j.appender.im.password = password log4j.appender.im.recipient =libin9ijava@163.com log4j.appender.im.layout=org.apache.log4j.PatternLayout log4j.appender.im.layout.ConversionPattern =[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n #应用于socket log4j.appender.SOCKET=org.apache.log4j.RollingFileAppender log4j.appender.SOCKET.RemoteHost=localhost log4j.appender.SOCKET.Port=5001 log4j.appender.SOCKET.LocationInfo=true # Set up for Log Facter 5 log4j.appender.SOCKET.layout=org.apache.log4j.PatternLayout log4j.appender.SOCET.layout.ConversionPattern=[start]%d{DATE}[DATE]%n%p[PRIORITY]%n%x[NDC]%n%t[THREAD]%n%c[CATEGORY]%n%m[MESSAGE]%n%n # Log Factor 5 Appender log4j.appender.LF5_APPENDER=org.apache.log4j.lf5.LF5Appender log4j.appender.LF5_APPENDER.MaxNumberOfRecords=2000 # 发送日志给邮件 log4j.appender.MAIL=org.apache.log4j.net.SMTPAppender log4j.appender.MAIL.Threshold=FATAL log4j.appender.MAIL.BufferSize=10 log4j.appender.MAIL.From=yyflyons@163 .com log4j.appender.MAIL.SMTPHost=www.wusetu.com log4j.appender.MAIL.Subject=Log4J Message log4j.appender.MAIL.To=libin9ijava@163.com log4j.appender.MAIL.layout=org.apache.log4j.PatternLayout log4j.appender.MAIL.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n
**
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> <properties resource="com/libin/conf/jdbc_oracle.properties"/> <!-- <typeAliases> <!– 设置别名 –> <typeAlias type="" alias="User"/> </typeAliases> --> <!-- 配置数据库连接的环境 --> <environments default="mysql"> <!--定义mysql环境 --> <environment id="mysql"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </dataSource> </environment> <!--定义oracle环境 --> <environment id="oracle"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </dataSource> </environment> </environments> <mappers> <!-- 引入Mapper.xml --> <!-- <package name=""/> --> <!--<mapper resource="com/ /Mapper.xml" /> --> </mappers> </configuration>
MybatisUtil.java
import java.io.IOException; import java.io.InputStream; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; public class MybatisUtil { private static SqlSessionFactory sqlSessionFactory = null; static { try { // 1.读取配置 final InputStream is = Resources .getResourceAsStream("mybatis-config.xml"); // 2.创建sqlSessionFactory MybatisUtil.sqlSessionFactory = new SqlSessionFactoryBuilder() .build(is); } catch (final IOException e) { e.printStackTrace(); } } private static final ThreadLocal<SqlSession> tol = new ThreadLocal<SqlSession>(); // 释放资源 public static void close() { // 获取sqlSession对象 final SqlSession sqlSession = MybatisUtil.tol.get(); if (sqlSession != null) { sqlSession.close(); MybatisUtil.tol.remove(); } } public static void commit() { final SqlSession session = MybatisUtil.openSession(); session.commit(); MybatisUtil.close(); } public static void commitone() { final SqlSession session = MybatisUtil.openSession(); session.commit(); } public static Object getMapper(final Class obj) { final SqlSession session = MybatisUtil.openSession(); return session.getMapper(obj); } // 提供获取sqlSession工具方法 public static SqlSession openSession() { SqlSession session = MybatisUtil.tol.get(); if (session == null) { // 创建sqlSession session = MybatisUtil.sqlSessionFactory.openSession(); // 放入本地线程变量 MybatisUtil.tol.set(session); } return session; } public static void rollback() { final SqlSession session = MybatisUtil.openSession(); session.rollback(); MybatisUtil.close(); } }
文末提供一波常用Maven依赖
<!--struts2-core依赖--> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>2.3.16</version> </dependency> <!--引入mybatis核心依赖--> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.6</version> </dependency> <!--引入mysql驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.49</version> </dependency> <!--引入jstl--> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!--引入Oracle的 JDBC 6--> <dependency> <groupId>com.oracle.database.jdbc</groupId> <artifactId>ojdbc6</artifactId> <version>11.2.0.4</version> </dependency>
感谢阅读,更多IDEA详细配置