Runnable JAR file spring + mybatis 普通项目(非maven 非springboot)

时间:2020-12-03 05:09:51
项目源码:https://github.com/dillyant/JAVA/tree/master/springmybatis 
准备环境: mysql数据库,测试数据库yanwftest
 
数据准备:
CREATE TABLE `user_t` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_name` varchar(40) NOT NULL,
  `password` varchar(255) NOT NULL,
  `age` int(4) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
 
 
INSERT INTO `user_t` VALUES ('1', '测试', 'sfasgfaf', '24');
 
 
 
  1. 创建java普通项目 File->New->Project...,选择Java Project
 
 
Runnable JAR file spring + mybatis 普通项目(非maven 非springboot)

 

 
 
2.项目名称自定义,以springmybatis为例,点击Finish。
Runnable JAR file spring + mybatis 普通项目(非maven 非springboot)

 

 
3.目录结构如下
 
Runnable JAR file spring + mybatis 普通项目(非maven 非springboot)

 

 
4. 调整目录结构
 
选择当前项目,右键New->Folder,添加lib目录
Runnable JAR file spring + mybatis 普通项目(非maven 非springboot)

 

 
 
选择当前项目,右键New->Source Folder,添加resources
Runnable JAR file spring + mybatis 普通项目(非maven 非springboot)

 

 
修改目录结构如下:
 
Runnable JAR file spring + mybatis 普通项目(非maven 非springboot)

 

 
 
5.lib目录下添加jar包,jar获取可以从 http://mvnrepository.com/maven仓库获取,   选中jar包右键,Build Path->Add To Build Path 
Runnable JAR file spring + mybatis 普通项目(非maven 非springboot)

 

6.src目录下添加源码jar包目录结构
Runnable JAR file spring + mybatis 普通项目(非maven 非springboot)

 

7.添加源码
 
包名:com.dillyant.model
类文件名:User.java
 
package com.dillyant.model;
public class User {
    private Integer id ;
    private String userName ;
    private String password ;
    private Integer age ;
    public Integer getId() {
        return id ;
    }
    public void setId(Integer id ) {
        this . id = id ;
    }
    public String getUserName() {
        return userName ;
    }
    public void setUserName(String userName ) {
        this . userName = userName == null ? null : userName .trim();
    }
    public String getPassword() {
        return password ;
    }
    public void setPassword(String password ) {
        this . password = password == null ? null : password .trim();
    }
    public Integer getAge() {
        return age ;
    }
    public void setAge(Integer age ) {
        this . age = age ;
    }
}
 
包名:com.dillyant.dao
类文件名:UserMapper.java
 
package com.dillyant.dao;
import com.dillyant.model.User;
public interface UserMapper {
    User selectByPrimaryKey(Integer id );
}
包名: com.dillyant.dao
XML文件名:UserMapper.xml
<? xml version = "1.0" encoding = "UTF-8" ?>
<! DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
< mapper namespace = "com.dillyant.dao.UserMapper" >
      < select id = "selectByPrimaryKey" parameterType = "java.lang.Integer"
           resultType = "com.dillyant.model.User" >
          select
          id, user_name, password, age
          from user_t
          where id =
          #{id,jdbcType=INTEGER}
      </ select >
</ mapper >
 
包名: com.dillyant.service
类文件名:IUserService.java
package com.dillyant.service;
import com.dillyant.model.User;
public interface IUserService {
      public User getUserById( int userId );
}
 
包名: com.dillyant.service.impl
类文件名:UserServiceImpl.java
 
package com.dillyant.service.impl;
 
import javax.annotation.Resource;
 
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import com.dillyant.dao.UserMapper;
import com.dillyant.model.User;
import com.dillyant.service.IUserService;
 
@Service ( "userService" )
@Transactional(readOnly=false)
public class UserServiceImpl implements IUserService {
    @Resource
    private UserMapper userMapper;
 
    @Override
    public User getUserById(int userId) {
        return this.userMapper.selectByPrimaryKey(userId);
    }
}
 
包名: com.dillyant.main
类文件名:Main.java
package com.dillyant.main;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.alibaba.fastjson.JSON;
import com.dillyant.model.User;
import com.dillyant.service.impl.UserServiceImpl;
public class   Main {
      private static Logger logger = Logger.getLogger(Main. class );
      private static ApplicationContext ac ;
      public static void main(String[] args ) {
           ac = new ClassPathXmlApplicationContext( "spring-mybatis.xml" );
          UserServiceImpl iuser = (UserServiceImpl) ac .getBean( "userService" );
          User user = iuser .getUserById(1);
           logger .info(JSON.toJSONString( user ));
     }
}
 
8 resouces目录下添加配置文件
目录结构如下:
Runnable JAR file spring + mybatis 普通项目(非maven 非springboot)

 

数据库配置文件jdbc.properties
 
 
jdbc.driver= com.mysql.jdbc.Driver
jdbc.url= jdbc:mysql://127.0.0.1:3306/yanwftest
jdbc.username=test
jdbc.password= test
#定义初始连接数 
jdbc.initialSize= 0
#定义最大连接数 
jdbc.maxActive= 20
#定义最大空闲 
jdbc.maxIdle= 20
#定义最小空闲 
jdbc.minIdle= 1
#定义最长等待时间 
jdbc.maxWait= 60000
 
 
 
日志配置文件
log4j.properties
 
 
#定义LOG输出级别
log4j.rootLogger= debug,Console,File
#定义日志输出目的地为控制台
log4j.appender.Console= org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target= System.out
#可以灵活地指定日志输出格式,下面一行是指定具体的格式
log4j.appender.Console.layout = org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern= [%c] - %m%n
#文件大小到达指定尺寸的时候产生一个新的文件
log4j.appender.File = org.apache.log4j.RollingFileAppender
#指定输出目录
log4j.appender.File.File = logs/sm.log
#定义文件最大大小
log4j.appender.File.MaxFileSize = 10MB
# 输出所以日志,如果换成DEBUG表示输出DEBUG以上级别日志
log4j.appender.File.Threshold = ALL
log4j.appender.File.layout = org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern = [%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c]%m%n
 
spring mybatis集成配置文件
spring-mybatis.xml
 
<? xml version = "1.0" encoding = "UTF-8" ?>
< beans xmlns = "http://www.springframework.org/schema/beans"
      xmlns:aop = "http://www.springframework.org/schema/aop" xmlns:tx = "http://www.springframework.org/schema/tx"
      xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns:mybatis = "http://mybatis.org/schema/mybatis-spring"
      xmlns:context = "http://www.springframework.org/schema/context"
      xsi:schemaLocation = "http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
          http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-4.1.xsd  
          http://mybatis.org/schema/mybatis-spring
          http://mybatis.org/schema/mybatis-spring.xsd
          http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context-4.1.xsd" >
      <!-- 自动扫描 -->
      < context:component-scan base-package = "com.dillyant.service.impl" />
      <!-- 引入配置文件 -->
      < context:property-placeholder location = "classpath:jdbc.properties"  system-properties-mode = "FALLBACK" />
      < bean id = "dataSource" class = "org.apache.commons.dbcp.BasicDataSource"
           destroy-method = "close" >
           < property name = "driverClassName" value = "${jdbc.driver}" />
           < property name = "url" value = "${jdbc.url}" />
           < property name = "username" value = "${jdbc.username}" />
           < property name = "password" value = "${jdbc.password}" />
           <!-- 初始化连接大小 -->
           < property name = "initialSize" value = "${jdbc.initialSize}" ></ property >
           <!-- 连接池最大数量 -->
           < property name = "maxActive" value = "${jdbc.maxActive}" ></ property >
           <!-- 连接池最大空闲 -->
           < property name = "maxIdle" value = "${jdbc.maxIdle}" ></ property >
           <!-- 连接池最小空闲 -->
           < property name = "minIdle" value = "${jdbc.minIdle}" ></ property >
           <!-- 获取连接最大等待时间 -->
           < property name = "maxWait" value = "${jdbc.maxWait}" ></ property >
      </ bean >
      <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
      < bean id = "sqlSessionFactory" class = "org.mybatis.spring.SqlSessionFactoryBean" >
           < property name = "dataSource" ref = "dataSource" />
           <!-- 自动扫描mapping.xml文件 -->
           < property name = "mapperLocations" value = "classpath:com/dillyant/dao/*.xml" ></ property >
      </ bean >
      <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
      < bean class = "org.mybatis.spring.mapper.MapperScannerConfigurer" >
           < property name = "basePackage" value = "com.dillyant.dao" />
           < property name = "sqlSessionFactoryBeanName" value = "sqlSessionFactory" ></ property >
      </ bean >
      <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
      < bean id = "transactionManager"
           class = "org.springframework.jdbc.datasource.DataSourceTransactionManager" >
           < property name = "dataSource" ref = "dataSource" />
      </ bean >
      <!-- 在Spring中使用基于注解的事务管理特性 -->
      < tx:annotation-driven transaction-manager = "transactionManager"
           proxy-target-class = "true" />
</ beans >
 
9。运行测试成功。
Runnable JAR file spring + mybatis 普通项目(非maven 非springboot)

 

 
10。导出可执行jar包
选中项目,右键Export,选择Runnable JAR file
Runnable JAR file spring + mybatis 普通项目(非maven 非springboot)

 

Launch configuration选择当前项目的main, Export destination自定义, Library handling选择第三项,其他两项测试都未通过。
Runnable JAR file spring + mybatis 普通项目(非maven 非springboot)

 

 
导出目录结果如下:
Runnable JAR file spring + mybatis 普通项目(非maven 非springboot)

 

 
(windows  平台)
使用java 命令执行该jar包,如下:
Runnable JAR file spring + mybatis 普通项目(非maven 非springboot)

 

 
结果如下,测试通过。
Runnable JAR file spring + mybatis 普通项目(非maven 非springboot)

 

 
 
linux 平台
使用java 命令执行该jar包,如下:
Runnable JAR file spring + mybatis 普通项目(非maven 非springboot)

 

结果如下,测试通过。
Runnable JAR file spring + mybatis 普通项目(非maven 非springboot)