程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA綜合教程 >> IDEA開發多項目的示例,idea開發示例

IDEA開發多項目的示例,idea開發示例

編輯:JAVA綜合教程

IDEA開發多項目的示例,idea開發示例


 前言:慢慢習慣用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上。

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved