码迷,mamicode.com
首页 > 其他好文 > 详细

IEDA开发多项目的示例

时间:2016-09-10 00:02:21      阅读:990      评论:0      收藏:0      [点我收藏+]

标签:

 前言:慢慢习惯用IDEA了,觉得用起来确实还不错,有VS的感觉,但是缺点就是每次启动慢,如果修改以后反应也需要好久,这方面做得不好。但是职能提示个人觉得要比Eclipse好太多。好了现在我用IDEA来开发一个简单的功能,希望给刚刚使用不太熟悉或者准备使用IDEA的开发者一点帮助。

1:需求

我们开发一个项目基本的需要一个domain层、dao层、Service层、web层。有的时候我们也需要comment层(存放一些公告调用的一些类)

2:创建项目

第一步:我们先创建一个父工程。

点击file然后选择new选择项目然后到下面界面

技术分享

然后按照他的提示下一步就成功的创建了父工程了。然后在pom中把packaging修改为pom,删除src整个目录。

第二步:创建子工程

找到你刚刚创建的父工程右击如下图

技术分享

选择以后出现如下图的界面选择你需要的原型,比如(如果你要创建web就可以选择web,也可以不选择)

技术分享

最后你对你的模块进行命名就可以了。

3:配置

请参考http://www.cnblogs.com/LipeiNet/p/5810251.html这个进行配置。

4:实战

主要功能就是添加用户到库中

在父工程的pom中把公共依然的jar引入,这是我引用的如下

<properties>
        <spring_version>3.1.0.RELEASE</spring_version>
    </properties>
 <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${spring_version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring_version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring_version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring_version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring_version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring_version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring_version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring_version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring_version}</version>
            <exclusions>
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>


        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring_version}</version>
            <exclusions>
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.6</version>
        </dependency>
        <dependency>
            <groupId>com.migr.common</groupId>
            <artifactId>migr-common-util</artifactId>
            <version>1.0</version>
        </dependency>

 然后在domain工程中可以定义自己的实体类比喻我的就是一个UserBean类如下

public class UserBean implements Serializable{
    private int id;//用户自增长id
    private String username;//用户名
    private String password;//密码
    private String realname;//真实姓名
    private int userroleid;//用户角色id;
    private Date createdate;//添加日期
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getRealname() {
        return realname;
    }

    public void setRealname(String realname) {
        this.realname = realname;
    }

    public int getUserroleid() {
        return userroleid;
    }

    public void setUserroleid(int userroleid) {
        this.userroleid = userroleid;
    }

    public Date getCreatedate() {
        return createdate;
    }

    public void setCreatedate(Date createdate) {
        this.createdate = createdate;
    }
}

在开始定义dao工程,在这里采用的是Mybatis,所以要把Mybatis jar包引进过来

 <dependency>
            <groupId>com.lp.rpc</groupId>
            <artifactId>web-domain</artifactId>
            <version>1.0</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.2.2</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.2.7</version>
        </dependency>
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.26</version>
        </dependency>

定义dao中的接口和实现类

public interface UserDao {
    /**
     * 添加用户
     * @param userBean 用户信息
     * @return
     */
    int add(UserBean userBean);

    /**
     * 验证用户是否存在
     * @param userBean 用户信息
     * @return
     */
    int isExistUser(UserBean userBean);
}
<?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.lp.rpc.dao.UserDao">
<insert id="add" parameterType="com.lp.web.beans.UserBean" keyProperty="id" useGeneratedKeys="true">
    INSERT INTO user(username,password,realname,userroleid,createdate)VALUES
    (#{username},#{password},#{realname},#{userroleid},#{createdate})
</insert>
    <select id="isExistUser" parameterType="com.lp.web.beans.UserBean" resultType="java.lang.Integer">
        SELECT COUNT(1) FROM user WHERE username=#{username} AND password=#{password}
    </select>
</mapper>

注意的是可以把这个dao写在resource中如下图

技术分享

竟然写完dao层了我们就要进行数据源的配置如下

spring-config-dao.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       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.xsd">
    <import resource="spring-config-datasource.xml"/>
    <!-- myBatis文件 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!-- 自动扫描entity目录, 省掉Configuration.xml里的手工配置 -->
        <property name="mapperLocations" value="classpath:sql/*.xml" />
    </bean>
    <!-- 注入映射的接口类 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.lp.rpc.dao" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
    </bean>
</beans>

spring-config-datasource.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       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.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
    <property name="url" value="jdbc:mysql://localhost:3306/usermanagerdb?useUnicode=true"></property>
    <property name="username" value="root"></property>
    <property name="password" value="123456"></property>
    <!--设置连接池初始值-->
    <property name="initialSize" value="5"></property>
    <!--设置连接池最大值-->
    <property name="maxActive" value="200"></property>
    <!--最大等待-->
    <property name="maxWait" value="10000"></property>
    <!--设置连接池最大空闲值-->
    <property name="maxIdle" value="10"></property>
    <!--设置连接池最小空闲值-->
    <property name="minIdle" value="5"></property>
</bean>
</beans>

Service工程在这里比较简单

public interface UserService {
   /**
    * 添加用户信息
    * @param userBean
    * @return
     */
   boolean add(UserBean userBean);

   /**
    * 验证用户是否存在
    * @param userBean
    * @return
     */
   boolean isExist(UserBean userBean);
}
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    public boolean add(UserBean userBean) {
        if (userDao.add(userBean)>=0){
            return true;
        }
        return false;
    }

    public boolean isExist(UserBean userBean) {
        if (userDao.isExistUser(userBean)>0){
            return true;
        }
        return false;
    }
}

最后我们进可以进行web工程了

pom中需要引用下面这几个依赖包

 <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>3.0-alpha-1</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>2.2.1</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

然后建立如下的目录

技术分享

然后add.jsp如下

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>添加用户</title>
</head>
<body>
<form action="/user/save" method="post">
    <table>
        <tr>
            <td>用户名:<input type="text" name="username"></td>
            <td>密码:<input type="text" name="password"></td>

        </tr>
        <tr>
            <td>真实姓名:<input type="text" name="realname"></td>
            <td>用户角色id:<input type="text" name="userroleid"></td>
        </tr>
        <tr>
            <td><input type="submit" value="提交"></td>
            <td><input type="reset" value="重置"></td>
        </tr>
    </table>
</form>
</body>
</html>

然后在定义一个UserController

package com.lp.rpc;

import com.lp.web.beans.UserBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

/**
 * Created by Administrator on 2016/9/2.
 */
@Controller
@RequestMapping(value = "/user/")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping(value = "dologin",method = RequestMethod.POST)
     public ModelAndView login(HttpServletRequest request, HttpServletResponse response) {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        UserBean userBean = new UserBean();
        userBean.setUsername(username);
        userBean.setPassword(password);
        if (userService.isExist(userBean)){
            return new ModelAndView("successful");
        }
        return new ModelAndView("add");
    }

    @RequestMapping(value = "add",method = RequestMethod.POST)
    public String add(){
        return "add";
    }
    @RequestMapping(value = "save",method = RequestMethod.POST)
    public String save(HttpServletRequest request, HttpServletResponse response)
    {
        String userName=request.getParameter("username");
        String password=request.getParameter("password");
        String userroleid=request.getParameter("userroleid");
        String realname=request.getParameter("realname");
        UserBean userBean=new UserBean();
        userBean.setUsername(userName);
        userBean.setPassword(password);
        userBean.setCreatedate(new Date());
        userBean.setRealname(realname);
        userBean.setUserroleid(Integer.parseInt(userroleid));
       if (userService.add(userBean)){
           return "successful";
       }
        return "error";
    }
}

然后进行配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
         xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <listener>
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
  </listener>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring/applicationContext.xml</param-value>
  </context-param>
  <!--springmvc开始-->
  <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring/applicationContext.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>
        

然后在进行配置Servlet解析器

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       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.xsd">
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 前缀 -->
        <property name="prefix" value="/WEB-INF/jsp/" />
        <!-- 后缀 -->
        <property name="suffix" value=".jsp" />
        <property name="contentType" value="text/html;charset=UTF-8" />
    </bean>
</beans>

最后把所有的配置文件导入applicationContext.xml中

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
       http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<import resource="classpath:spring-config-dao.xml"></import>
    <import resource="spring-servlet.xml"></import>
    <context:component-scan base-package="com.lp.*"/>
    <context:annotation-config/>
</beans>

演示结果:

技术分享

然后用zhangsan(库中不存在)去登陆就会跳转到添加用户页面

技术分享

点击提交

技术分享

如果在添加的时候java不是资源目标记得要转换下

技术分享

ok结束了,主要流程基本就是这样的,如果没有使用过IDEA的或者初学者按照写一遍应该会明白IDEA的基本使用。源码等把基于rest的rpc写完了以后一起上传github上。

 

IEDA开发多项目的示例

标签:

原文地址:http://www.cnblogs.com/LipeiNet/p/5856414.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!