700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > maven打普通包jar包(依赖一并打入)

maven打普通包jar包(依赖一并打入)

时间:2023-07-27 04:18:24

相关推荐

maven打普通包jar包(依赖一并打入)

1. 创建一个maven项目

这里可以看到新创建的maven项目

2. 在pom.xml添加项目需要的依赖

<dependencies><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.2.5</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.16</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.80</version></dependency></dependencies>

3. 在pom.xml中加入打包插件

<build><plugins><plugin><artifactId>maven-assembly-plugin</artifactId><configuration><descriptorRefs><descriptorRef>jar-with-dependencies</descriptorRef></descriptorRefs><archive><manifest><mainClass>com.jamy.song.job.Demo</mainClass></manifest></archive></configuration><executions><execution><id>make-assembly</id><phase>package</phase><goals><goal>single</goal></goals></execution></executions></plugin></plugins></build>

4. 编写代码和添加配置文件

主类

public class Demo {public static void main(String[] args) {System.out.println("hello world");System.out.println("hello world");System.out.println("hello world");System.out.println("你好,世界!");UserDao dao = new UserDao(JDBCUtil.getConn(), null);try {dao.insertData();System.out.println("执行dao方法成功");} catch (SQLException e) {System.out.println("执行dao方法报错!!!");throw new RuntimeException(e);}}}

JDBC工具类

public class JDBCUtil {private static final ThreadLocal<Connection> threadLocal = new ThreadLocal<Connection>();private static Properties prop = new Properties();//private static ComboPooledDataSource dataSource = new ComboPooledDataSource();private static DruidDataSource dataSource = new DruidDataSource();private static Connection conn = null;static {//数据库连接初始化try{prop.load(JDBCUtil.class.getClassLoader().getResourceAsStream("jdbc.properties"));}catch(IOException e){System.out.println("数据库参数文件读取错误");e.printStackTrace();}/*dataSource.setUser(prop.getProperty("jdbc.username"));dataSource.setJdbcUrl(prop.getProperty("jdbc.url"));dataSource.setPassword(prop.getProperty("jdbc.password"));*/dataSource.setUsername(prop.getProperty("jdbc.username"));dataSource.setUrl(prop.getProperty("jdbc.url"));dataSource.setPassword(prop.getProperty("jdbc.password"));try{//dataSource.setDriverClass(prop.getProperty("jdbc.driverClass"));dataSource.setDriverClassName(prop.getProperty("jdbc.driverClass"));}catch(Exception e){System.out.println("连接池构造异常1!");e.printStackTrace();}/*dataSource.setInitialPoolSize(Integer.valueOf(prop.getProperty("jdbc.initialSize")));dataSource.setMinPoolSize(Integer.valueOf(prop.getProperty("jdbc.minPoolSize")));dataSource.setMaxPoolSize(Integer.valueOf(prop.getProperty("jdbc.maxPoolSize")));dataSource.setMaxStatements(Integer.valueOf(prop.getProperty("jdbc.MaxStatements")));dataSource.setMaxIdleTime(Integer.valueOf(prop.getProperty("jdbc.MaxIdleTime")));*/dataSource.setInitialSize(Integer.valueOf(prop.getProperty("jdbc.initialSize")));dataSource.setMinIdle(Integer.valueOf(prop.getProperty("jdbc.minPoolSize")));dataSource.setMaxActive(Integer.valueOf(prop.getProperty("jdbc.maxPoolSize")));dataSource.setMaxWait(Integer.valueOf(prop.getProperty("jdbc.maxWait")));dataSource.setTimeBetweenEvictionRunsMillis(Integer.valueOf(prop.getProperty("jdbc.timeBetweenEvictionRunsMillis")));dataSource.setMinEvictableIdleTimeMillis(Integer.valueOf(prop.getProperty("jdbc.minEvictableIdleTimeMillis")));try {threadLocal.set(dataSource.getConnection());} catch (SQLException e) {throw new RuntimeException(e);}}//获取连接public static Connection getConn(){conn = threadLocal.get();System.out.println("threadLocal获取连接:" + conn);try{if (conn == null || conn.isClosed()) {Class.forName(prop.getProperty("jdbc.driverClass")).toString();conn = DriverManager.getConnection(prop.getProperty("jdbc.url").toString(),prop.getProperty("jdbc.username").toString(),prop.getProperty("jdbc.password").toString());try{//dataSource.setDriverClass(prop.getProperty("jdbc.driverClass"));dataSource.setDriverClassName(prop.getProperty("jdbc.driverClass"));}catch(Exception e){System.out.println("连接池构造异常2!");e.printStackTrace();throw new IllegalStateException();}}System.out.println("conn:"+conn);}catch(Exception e){System.out.println("获取数据库连接异常!");e.printStackTrace();throw new IllegalStateException();}return conn;}//关闭连接public static void closeConn(){try{if(threadLocal.get() != null && !threadLocal.get().isClosed()){threadLocal.get().close();threadLocal.remove();}}catch(Exception e){System.out.println("关闭数据库连接异常!");}}//事务开始public static void beginTransaction(){try{getConn().setAutoCommit(false);}catch(SQLException e){System.out.println("开始事务异常");throw new IllegalStateException();}}//事务提交public static void commit(){try{getConn().commit();}catch(SQLException e){System.out.println("提交事务异常");throw new IllegalStateException();}}//事务回滚public static void rollback(){try{Connection conn = getConn();conn.setAutoCommit(false);conn.rollback();conn.setAutoCommit(true);}catch(SQLException e){System.out.println("事务回滚异常");throw new IllegalStateException();}}}

JDBC操作类

public class UserDao {private Connection conn;private JSONObject json;PreparedStatement ps = null;public UserDao(Connection conn, JSONObject json) {this.conn = conn;this.json = json;}public void insertData() throws SQLException {String sql = "insert into tb_user(name,age,address) values(?,?,?)";ps = conn.prepareStatement(sql);try{for (int i=0; i<100; i++) {ps.setString(1, "张三"+i);ps.setInt(2, 23);ps.setString(3, "人民路120号");ps.addBatch();}ps.executeBatch();}catch(SQLException e){System.out.println("执行插入数据异常!");}finally {if (ps != null) {ps.close();}if (conn != null){conn.close();}}}}

配置文件

jdbc.driverClass=com.mysql.cj.jdbc.Driverjdbc.url=jdbc:mysql://192.168.227.122:3306/test?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=GMT%2b8&nullCatalogMeansCurrent=true&allowPublicKeyRetrieval=truejdbc.username=rootjdbc.password=rootjdbc.initialSize=20jdbc.minPoolSize=10jdbc.maxPoolSize=100#jdbc.MaxStatements=5#jdbc.MaxIdleTime=600jdbc.maxWait=60000jdbc.timeBetweenEvictionRunsMillis=60000jdbc.minEvictableIdleTimeMillis=300000

5. 打jar包

这里用解压缩文件打开jar包看到,依赖和业务类都有,说明我们打的jar包是完整的

这的jar包名字太长了,这里我把它改成jamy.jar,然后把他上传到linux服务器上(要放在shell脚本里写的位置,否则执行时找不到),我们通过shell脚本来执行下看看,脚本内容如下:

#!/bin/bashJAVA_HOME=/usr/local/src/jdk1.8LANG=zh_CN.UTF-8BATCHROOT=/app/demoBatch${JAVA_HOME}/bin/java -Xms1024M -Xmx1024M -Dfile.encoding=UTF-8 -cp ${BATCHROOT}/jamy.jar com.jamy.song.job.Demo

现在来看下执行效果(执行前jdk要配置好,我这里配置的时jdk1.8),执行sh demo2.sh这个命令即可

注意:这种打jar包非常适合批处理业务,如果是纯java项目可以参考下面两种方法:

纯java项目打包方法一

纯java项目打包方法二

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。