完成向後臺添加用戶的ssm項目,完整版

1:ssm框架整合javascript

  1.1添加maven依賴pom.xmlcss

  

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.liyafei</groupId>
    <artifactId>ssmFrameworkIntergrate</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <properties>
        <junit.version>4.12</junit.version>
        <spring.version>4.1.3.RELEASE</spring.version>
        <mybatis.version>3.2.8</mybatis.version>
        <mybatis.spring.version>1.2.2</mybatis.spring.version>
        <mybatis.paginator.version>1.2.15</mybatis.paginator.version>
        <mysql.version>5.1.32</mysql.version>
        <slf4j.version>1.6.4</slf4j.version>
        <jackson.version>2.4.2</jackson.version>
        <druid.version>1.0.9</druid.version>
        <httpclient.version>4.3.5</httpclient.version>
        <jstl.version>1.2</jstl.version>
        <servlet-api.version>2.5</servlet-api.version>
        <jsp-api.version>2.0</jsp-api.version>
        <joda-time.version>2.5</joda-time.version>
        <commons-lang3.version>3.3.2</commons-lang3.version>
        <commons-io.version>1.3.2</commons-io.version>
        <commons-net.version>3.3</commons-net.version>
        <pagehelper.version>3.4.2-fix</pagehelper.version>
        <jsqlparser.version>0.9.1</jsqlparser.version>
        <commons-fileupload.version>1.3.1</commons-fileupload.version>
        <jedis.version>2.7.2</jedis.version>
        <solrj.version>4.10.3</solrj.version>
    </properties>

    <!-- <dependencyManagement></dependencyManagement>依賴管理,將會不實際依賴,只是管理版本號 -->
    <!-- 只定義依賴的版本,並不實際依賴 -->
    <dependencies>
        <!-- 時間操做組件 -->
        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
            <version>${joda-time.version}</version>
        </dependency>
        <!-- Apache工具組件 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>${commons-lang3.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-io</artifactId>
            <version>${commons-io.version}</version>
        </dependency>
        <dependency>
            <groupId>commons-net</groupId>
            <artifactId>commons-net</artifactId>
            <version>${commons-net.version}</version>
        </dependency>
        <!-- Jackson Json處理工具包 -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>${jackson.version}</version>
        </dependency>
        <!-- httpclient -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>${httpclient.version}</version>
        </dependency>
        <!-- 單元測試 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <!-- 日誌處理 -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <!-- Mybatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>${mybatis.version}</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>${mybatis.spring.version}</version>
        </dependency>
        <dependency>
            <groupId>com.github.miemiedev</groupId>
            <artifactId>mybatis-paginator</artifactId>
            <version>${mybatis.paginator.version}</version>
        </dependency>
        <!-- MySql -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.version}</version>
        </dependency>
        <!-- 鏈接池 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>${druid.version}</version>
        </dependency>
        <!-- Spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</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-webmvc</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-aspects</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!-- JSP相關 -->
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>${jstl.version}</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>${servlet-api.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jsp-api</artifactId>
            <version>${jsp-api.version}</version>
            <scope>provided</scope>
        </dependency>
        <!-- 文件上傳組件 -->
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>${commons-fileupload.version}</version>
        </dependency>
        <!-- Redis客戶端 -->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>${jedis.version}</version>
        </dependency>
        <!-- solr客戶端 -->
        <dependency>
            <groupId>org.apache.solr</groupId>
            <artifactId>solr-solrj</artifactId>
            <version>${solrj.version}</version>
        </dependency>
    </dependencies>
    <build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <!-- 資源文件拷貝插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.7</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <!-- java編譯插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.2</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <!-- 配置Tomcat插件 -->
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <port>8080</port>
                    <path>/</path>
                </configuration>
            </plugin>
        </plugins>

        <!-- 若是不添加此節點mybatis的mapper.xml文件都會被漏掉。 -->

    </build>
</project>

  1.2:配置mybatis 底層文件html

  SqlMapConfig.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>
     <mappers>
         <mapper resource="mapper/RoleMapper.xml"/>  <!--mapper映射-->
     </mappers>
</configuration>


   1.3配置applicationContext.xml,將applicaitonContext分紅dao層service層,transaction,mvcjava

  1.3.1首先配置properties文件mysql

    jdbc.propertiesjquery

#springboot \u6574\u5408mybatis\u6846\u67B6
#\u626B\u63CF\u6620\u5C04\u6587\u4EF6
#mybatis.mapper-locations=classpath:mapper/*Mapper.xml
#\u52A0\u8F7Dconfig\u6587\u4EF6
#mybatis.config-location=classpath:mapper/mybatis-config.xml
#\u5B9A\u4E49\u522B\u540D,\u4E0E\u6570\u636E\u5E93\u8868\u4E00\u4E00\u5BF9\u5E94
mybatis.type-aliases-package=com.liyafei.pojo

#\u52A0\u8F7D\u6570\u636E\u6E90
jdbc.url=jdbc:mysql://localhost:3306/demo
jdbc.driver=com.mysql.jdbc.Driver
jdbc.username=root
jdbc.password=1367356

 

  1.3.2    applicationContext-dao.xmlgit

     

<?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: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:property-placeholder location="classpath:resource/jdbc.properties" />
    <!-- 數據庫鏈接池 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
        destroy-method="close">
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="maxActive" value="10" />
        <property name="minIdle" value="5" />
    </bean>
    <!-- 配置sqlsessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"></property>
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!-- 配置掃描包,加載mapper代理對象 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.li.mapper"></property>
    </bean>
</beans>

    1.3.3 applicationContext-service.xmlgithub

  

<?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: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">

    <!-- 掃描包加載Service實現類 -->
    <context:component-scan base-package="com.li.service"></context:component-scan>
</beans>

1.3.4   applicationContext-transaction.xmlweb

  

<?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: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">

    <!-- 事務管理器 -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!-- 數據源 -->
        <property name="dataSource" ref="dataSource" />
    </bean>
    <!-- 通知 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <!-- 傳播行爲 -->
            <tx:method name="save*" propagation="REQUIRED" />
            <tx:method name="insert*" propagation="REQUIRED" />
            <tx:method name="add*" propagation="REQUIRED" />
            <tx:method name="create*" propagation="REQUIRED" />
            <tx:method name="delete*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="find*" propagation="SUPPORTS" read-only="true" />
            <tx:method name="select*" propagation="SUPPORTS" read-only="true" />
            <tx:method name="get*" propagation="SUPPORTS" read-only="true" />
        </tx:attributes>
    </tx:advice>
    <!-- 切面 -->
    <aop:config>
        <aop:advisor advice-ref="txAdvice"
            pointcut="execution(* com.taotao.service.*.*(..))" />
    </aop:config>
</beans>

1.3.5   springmvc.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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 配置表現層(controller)掃描包 -->
    <context:component-scan base-package="com.li.controller" />

    <!-- 配置註解驅動 -->
    <mvc:annotation-driven />
    <!-- 配置視圖解析器(返回modelandview的name。將會尋找相應的jsp文件) -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <!-- 資源映射 ,web.xml攔截全部的請求,包括靜態資源, -->
    <!-- 當攔截了/css/**"/,就映射到工程中/WEB-INF/css/ 這個位置 -->
    <mvc:resources location="/WEB-INF/css/" mapping="/css/**" />
    <mvc:resources location="/WEB-INF/js/" mapping="/js/**" />
    <mvc:resources location="/WEB-INF/html/" mapping="/html/**" />
    <mvc:resources location="/WEB-INF/jsp/" mapping="/jsp/**" />
</beans>


   1.3.6 web.xml

  

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="taotao" version="2.5">
    <display-name>taotao-manager</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
    <!-- 加載spring容器 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/applicationContext-*.xml</param-value>  <!-- spring中配置的xml文件將會被加載 -->
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!-- 解決post亂碼 -->
    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>utf-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <!-- springmvc的前端控制器 -->
    <servlet>
        <servlet-name>taotao-manager</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        
        <!-- contextConfigLocation不是必須的, 若是不配置contextConfigLocation, springmvc的配置文件默認在:WEB-INF/servlet的name+"-servlet.xml" -->
        <!-- 加載springmvc.xml文件 -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/springmvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    
    <!-- /攔截全部的請求路徑(包括靜態資源), 請求路徑將通過springmvc.xml,須要作靜態資源映射-->
    <servlet-mapping>
        <servlet-name>taotao-manager</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

           2:建立sql,pojo,Mapper,mapper.xml,service。controller等

    2.1在demo(jdbc.properties文件裏有配置)數據庫下建立sql

create table role(id int not null,
                        roleName varchar(60),
                        note  varchar(60));

 2. 2建立對應的 POJO

     Role.java

  

package com.li.pojo;

public class Role {

    private int id;
    private String roleName;
    private String note;

       setter and getter
}

   2.3建立RoleMapper.java

  

package com.li.mapper;

import org.apache.ibatis.annotations.Param;

import com.li.pojo.Role;

public interface RoleMapper {
    public int addRole(@Param("r")Role r);
}

2.4建立Mapper對應的映射文件RoleMapper.xml,該RoleMapper.xml若是放置到src/main/java目錄下面,將會不能被加載,出現invalid bound  。由於src/main/java下面的文件被打成class文件。因此下面只能是.java文件。若是放置其它文件。要在pom.xml下面添加依賴:http://www.cnblogs.com/liyafei/p/7911745.html

    若是該maven工程其它目錄下還含有其它文件。那麼最好不要將Mapper.xml放置在src/main/java下面

    RoleMapper.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.li.mapper.RoleMapper" >
    <insert id="addRole" parameterType="com.li.pojo.Role">
        insert into role values(#{r.id},#{r.roleName},#{r.note})
    </insert>
</mapper>

2.5   建立RoleService.java

  

package com.li.service;

import org.springframework.stereotype.Service;

import com.alibaba.druid.filter.AutoLoad;
import com.li.pojo.Role;

public interface RoleService {
    public int addRole(Role role);
}

2.6建立RoleServiceImple.java

package com.li.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import com.li.mapper.RoleMapper;
import com.li.pojo.Role;

@Component
public class RoleServiceImpl implements RoleService{
    @Autowired
    private RoleMapper roleMapper;
    public int addRole(Role role){
        return roleMapper.addRole(role);
        
    }
}

2.7建立RoleController.java

package com.li.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import *
@Controller
@RequestMapping("/params")
public class ParamsController {
    @Autowired
    private RoleService roleService;
    /**
     * ʵ���ض�����
     */
    @RequestMapping("/addRole")
    //ModelΪ�ض�������ģ�ͣ�Spring MVC���Զ���ʼ����
    public String addRole(Model model,String roleName,String note){
        Random random=new Random(47);
        int id=random.nextInt(1000);
        System.out.println(id);
        

        UUID randomUUID = UUID.randomUUID();
        int id1 = randomUUID.hashCode();
        Role role=new Role();
        role.setRoleName(roleName);
        role.setNote(note);
        role.setId(id1);
        //�����ɫ�󣬻����ɫ��š�
        roleService.addRole(role);
        //���ض�������ģ��
        model.addAttribute("note", note);
        model.addAttribute("roleName", roleName);
        model.addAttribute("id", role.getId());
        return "redirect:./showRoleJsonInfo.do"; //������ģ���ض���showRoleJsonInfo.do
    }
}


3:建立前臺頁面

  addRole.html

  

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="https://code.jquery.com/jquery-3.2.0.js">
</script>
<script type="text/javascript">
//提交序列化表單
$(document).ready(function(){
        $("#commit").click(function() {
            var str=$("form").serialize();
            //提交表單
            $.post({url:"/params/addRole.do",
            //將form數據序列化,傳遞給後臺,則將數據以roleName=xxx&&note=xxx傳遞
             data:str, 
             //成功後的方法
             success:function(result) {    
            }
            });
        });
    });
</script>
</head>
<body>
    <form id="form">
        <table>
            <tr>
                <td>角色名稱</td>
                <td><input id="roleName" name="roleName"/></td>
            </tr>
            <tr>
                <td>備註</td>
                <td><input id="note" name="note"/></td>
            </tr>
            <tr>
                <td></td>
                <td align="right"><input id="commit" type="button" value="添加用戶"/></td>
            </tr>
        </table>
    </form>
</body>
</html>

4:測試成功:

  5:目錄結構:

 

相關文章
相關標籤/搜索