在公司内网环境下用Intellij IDEA创建springMVC-Mybatis项目

刚使用IDEA工具编程,有很多不会,找了许多的文章,终于搭建成功了。在此记录一下自己创建时遇到的所有问题及解决的过程。
在公司内网环境中需要使用代理才能访问外网的情况下。

一、首先设置setting.xml 加入

setting.xml

<proxies>

<!--配置代理-->

    <proxy>
      <id>optional</id>
      <active>true</active>
      <protocol>http</protocol>
      <username>proxyuser</username>

      <password>proxypass</password>

    <!--配置自己公司的代理地址-->

      <host>proxy.host.net</host>

    <!--端口号-->

      <port>80</port>
      <nonProxyHosts>local.net|some.host.com</nonProxyHosts>

    </proxy>

<!--其他默认即可-->

  </proxies>

 <mirrors>

<!--配置镜像-->

     <mirror>
      <id>mirrorId</id>
      <mirrorOf>repositoryId</mirrorOf>
      <name>Human Readable Name for this Mirror.</name>
      <url>http://maven.aliyun.com/nexus/content/groups/public</url>
    </mirror>
  </mirrors>

二、设置Maven如图




做完以上的步骤后,maven配置完成。下面创建Maven项目。

三、创建Maven项目


之后给项目起名字,没什么注意的地方,一路点击NEXT,创建成功,如图:



四、导包(由于属于内网环境,虽然配置了代理,有时也可能会失败,在此我选择手动导包)

1.在src的同级目录下创建Lib文件夹,用于保存手动导入的jar包,将下载好的jar包复制到lib文件夹中,

回到IDEA如图所示。

   

此时jar包还不能使用,需要在导入。请参考http://blog.csdn.net/a153375250/article/details/50851049

导包成功后如图,导入的jar包可以打开了。


此时基础已经完成了。

五、配置spring和Mybatis配置文件


下面是配置文件的详细内容:

config.properties
driverclass=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@localhost:1521:SID
username=test
password=test01
maxActive=100
maxWait=5000
initialSize=10
minIdle=1
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>
<!--扫描Mapper.xml文件的地址-->
<mappers>
    <!--<package name="mappers"></package>-->
    <mapper resource="mappers/CyOUserExtendMapper.xml"></mapper>
</mappers>
</configuration>
spring-mybatis.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.0.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
       http://www.springframework.org/schema/util
       http://www.springframework.org/schema/util/spring-util-4.0.xsd">
    <!-- 自动扫描 -->
    <context:component-scan base-package="dao" />
    <!-- 引入配置文件 -->
    <bean id="propertyConfigurer"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="classpath:config.properties" />
    </bean>
    <!--<util:properties id="cfg" location="classpath:config.properties"/>-->
    <!--配置DruidDataSource连接池 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
          destroy-method="close">
        <property name="driverClassName" value="${driverclass}"/>
        <property name="url" value="${url}"/>
        <property name="username" value="${username}"/>
        <property name="password" value="${password}"/>
        <!-- 初始化连接大小 -->
        <property name="initialSize" value="${initialSize}" />
        <!-- 连接池最大数量 -->
        <property name="maxActive" value="${maxActive}"/>
        <!-- 连接池最小空闲 -->
        <property name="minIdle" value="${minIdle}" />
        <!-- 获取连接最大等待时间 -->
        <property name="maxWait" value="${maxWait}"/>
    </bean>

    <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!-- 自动扫描mapping.xml文件 -->
        <!--<property name="mapperLocations" value="classpath:mappers/*.xml"></property>-->
        <!-- 指定mybatis核心配置文件 -->
        <property name="configLocation" value="classpath:mybatis-config.xml"></property>
    </bean>
    <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="dao.AbstractClass" />
        <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>
    <bean id="CyOUserExtendMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <!--配置mapper接口的全路径名称 -->
        <property name="mapperInterface" value="dao.CyOUserExtendMapper"></property>
        <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
    </bean>
</beans>
CyOUserExtendMapper.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="dao.AbstractClass.CyOUserExtendMapper">
    <!--namespace与接口的路径一致-->
  <!--id名与接口的方法名对应,resultType与实体类对应-->
    <select id="selectAll" parameterType="Object" resultType="dao.entity.Cy_O_User_Extend">
        SELECT * FROM 表名
    </select>

</mapper>

测试:

test.class
import dao.CyOUserExtendMapper;
import entity.Cy_O_User_Extend;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;


public class test{

    @Test
    public void testSpring_My(){
        ApplicationContext ac=new ClassPathXmlApplicationContext(new String[] {"spring-mybatis.xml"});
        CyOUserExtendMapper coued=(CyOUserExtendMapper) ac.getBean("CyOUserExtendMapper");
        List<Cy_O_User_Extend> cy_o_user_extendList=coued.selectAll();
        for (Cy_O_User_Extend coue: cy_o_user_extendList) {
            System.out.println(coue);
            break;
        }
    }
}

在此就不将,接口和实体类代码写出了。

结束。

我在搭建的过程中遇到很多问题,也找了许多文章,在此感谢诸位大神。

在以上内容中出现什么解释不对的地方,请多多指教。谢谢^_^


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值