Idea uses Maven to build a SSM primer

Source: Internet
Author: User
Tags aliases connection pooling log4j

Recently used idea, I used to use myeclipse, directly into the jar package to build the framework, now found idea using MAVEN to build a framework particularly fast, nonsense not to say, directly on the map (picture a bit more, please forgive me).

Pom.xml

<?xml version= "1.0" encoding= "UTF-8"?> <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.mcd </groupId> <artifactId>ssmCrud</artifactId> <version>1.0-SNAPSHOT</version> < packaging>war</packaging> <name>ssmcrud Maven webapp</name> <url>http:// Maven.apache.org</url> <properties> <project.build.sourceencoding>utf-8</ Project.build.sourceencoding> <project.reporting.outputencoding>utf-8</ Project.reporting.outputencoding> <spring.version>5.0.3.RELEASE</spring.version> <
    Mybatis.version>3.4.4</mybatis.version> </properties> <dependencies> <!--unit Test--> <dependency> <gRoupid>junit</groupid> <artifactId>junit</artifactId> <version>3.8.1</version&gt
      ;
    <scope>test</scope> </dependency> <!--Part I: Spring configuration--> <!--Spring Core--> <dependency> <groupId>org.springframework</groupId> <artifactid>spring-core</arti factid> <version>${spring.version}</version> </dependency> <dependency> ;groupid>org.springframework</groupid> <artifactId>spring-beans</artifactId> <version& gt;${spring.version}</version> </dependency> <dependency> <groupid>org.springframewo Rk</groupid> <artifactId>spring-context</artifactId> <version>${spring.version}</ve rsion> </dependency> <dependency> <groupId>org.springframework</groupId> &L T;artifactid>Spring-context-support</artifactid> <version>${spring.version}</version> </dependency> <!--Spring DAO--> <dependency> <groupId>org.springframework</groupId> <art
    Ifactid>spring-jdbc</artifactid> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactid>spring-tx</artif
    Actid> <version>${spring.version}</version> </dependency> <!--spring MVC--> <dependency> <groupId>org.springframework</groupId> <artifactid>spring-web</artifa ctid> <version>${spring.version}</version> </dependency> <dependency> <g Roupid>org.springframework</groupid> <artifactId>spring-webmvc</artifactId> &LT;VERSION&G t;${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependenc
      Y> <!--Part II: Servlet Web--> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.0.1</version> <scope>provi
      ded</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.2</version> <scope>provided</ scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <arti
      factid>jstl</artifactid> <version>1.2</version> </dependency> <dependency> <groupid>taglibs</groupid> <artifactId>standard</artifactId> <version>1.1.2</version> &L t;/dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> &LT;ARTIFAC Tid>jackson-databind</artifactid> <version>2.9.4</version> </dependency> <!-- Part III: Database and MyBatis--> <!--database--> <dependency> <groupId>mysql</groupId> 
    ;artifactid>mysql-connector-java</artifactid> <version>5.1.38</version> </dependency> <!--database connection pool--> <dependency> <groupId>com.mchange</groupId> <artifactid&gt
    ;c3p0</artifactid> <version>0.9.5.2</version> </dependency> <!--mybatis-->
      <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mYbatis.version}</version> </dependency> <!--mybatis-spring Integration Package--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version> 1.3.1</version> </dependency> <!--Part IV: Log--> <!--implement SLF4J interface and integrate--> <dependenc
      Y> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.1.1</version> </dependency> </dependencies> <build> &LT;FINALNAME&G t;ssmcrud</finalname> <plugins> <plugin> <groupid>org.apache.maven.plugins</g Roupid> <artifactId>maven-compiler-plugin</artifactId> <configuration> < source>1.8</source> <target>1.8</target> </configuration> &LT;/PLUGIN&G
    T
</plugins>  </build> </project>
 

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>
    <!--configuring global properties-->
    <settings>
        <!-- Use JDBC Getgeneratedkeys to get database self-added primary key value-->
        <setting name= "Usegeneratedkeys" value= "true"/>

        <!-- Replace column names with column aliases default: True-->
        <setting name= "Usecolumnlabel" value= "true"/>

        <!--Open Hump name conversion: Table{create _time}-> Entity{createtime}-->
        <setting name= "Mapunderscoretocamelcase" value= "true"/>
    < /settings>
</configuration>
Jdbc.properties
Jdbc.driver=com.mysql.jdbc.driver
#自己对应的数据库
jdbc.url=jdbc:mysql://127.0.0.1:3306/mybatis
Jdbc.username=root
jdbc.password=123

Log4j.properties

Log4j.rootlogger=error, stdout
log4j.appender.stdout=org.apache.log4j.consoleappender
Log4j.appender.stdout.layout=org.apache.log4j.patternlayout
Log4j.appender.stdout.layout.ConversionPattern =%5p [%t]-%m%n

Spring-dao.xml

<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "Http://www.springframework.org/schema/beans" xmlns:x
       Si= "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.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context.xsd "> <!--configuration integration MyBatis Process--> <!--1. Metabase Related parameters properties: ${url}--> <contex  T:property-placeholder location= "Classpath:jdbc.properties"/> <!--2. Database connection pool--> <bean id= "DataSource" class= "Com.mchange.v2.c3p0.ComboPooledDataSource" > <!--Configure connection pooling properties--> <property name= "Drivercla SS "Value=" ${jdbc.driver} "/> <property name=" Jdbcurl "value=" ${jdbc.url} "/> <property name=" us
    Er "value=" ${jdbc.username} "/>    <property name= "Password" value= "${jdbc.password}"/> <!--c3p0 Connection pool private properties--> <property N Ame= "maxpoolsize" value= "/> <property name=" minpoolsize "value=" "/> <!--close the connection without automatic commit- -> <property name= "Autocommitonclose" value= "false"/> <!--get connection timeout--> <propert Y name= "checkouttimeout" value= "10000"/> <!--when get Connection failed retry number--> <property name= "Acquireretryatte MPTs "value=" 2 "/> </bean> <!--3. Configure Sqlsessionfactory object--> <bean id=" Sqlsessionfactory "CLA ss= "Org.mybatis.spring.SqlSessionFactoryBean" > <!--inject database connection pool--> <property name= "DataSource" R  ef= "DataSource"/> <!--configuration mybaties Global profile: Mybatis-config.xml--> <property name= "Configlocation" Value= "Classpath:mybatis-config.xml"/> <!--scan Pojo packets using aliases--> <property name= "Typealiasespacka GE "value=" Com.mcd.pojo "/> <!--Scan SQL configuration file: Mapper required XML file--> <property name= "Mapperlocations" value= *.xml "/> </bean> <!--4. Configure Scan DAO Interface Pack, dynamically implement DAO interface, inject into spring container--> <bean class=" Org.mybatis.spri Ng.mapper.MapperScannerConfigurer "> <!--injected sqlsessionfactory--> <property name=" Sqlsessionfac Torybeanname "value=" sqlsessionfactory/> <!--gives you the need to scan DAO interface Packs--> <property name= "Basepackage" V Alue= "Com.mcd.dao"/> </bean> </beans>

Spring-mvc.xml

<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "Http://www.springframework.org/schema/beans" xmlns:x
       Si= "Http://www.w3.org/2001/XMLSchema-instance" 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/ Context Http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/ MVC http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd "> <!--configuration Springmvc--> <!--1. Open s
        PRINGMVC annotation Mode--> <!--simplified configuration: (1) Automatic registration Defaultanootationhandlermapping,anotationmethodhandleradapter

    (2) Provide some columns: data binding, numeric and date format @NumberFormat, @DateTimeFormat, xml,json default read-write support--> <mvc:annotation-driven/>
       <!--2. Static resource default servlet configuration (1) Adding to the processing of static resources: Js,gif,png (2) allows the use of "/" to do the overall mapping--> <mvc:default-servlet-handler/> <!--3. Configure JS
        P Displays viewresolver--> <bean class= "Org.springframework.web.servlet.view.InternalResourceViewResolver" > <property name= "Viewclass" value= "Org.springframework.web.servlet.view.JstlView"/> <property name= "pref" IX "value="/web-inf/jsp/"/> <property name=" suffix "value=". jsp "/> </bean> <!--4. Scan W EB-related bean--> <context:component-scan base-package= "Com.mcd.controller"/> </beans>

Spring-service.xml

<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "Http://www.springframework.org/schema/beans" Xmlns:xs
       I= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:context= "Http://www.springframework.org/schema/context" xmlns:tx= "Http://www.springframework.org/schema/tx" xsi:schemalocation= "http://www.springframework.org/schema/
    Beans Http://www.springframework.org/schema/beans/spring-beans.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/context/spring-context.xsd Http://www.springframework.org/schema/tx http ://www.springframework.org/schema/tx/spring-tx.xsd > <!--scan All types of annotations under service packs--> <context:component- Scan base-package= "Com.mcd.service"/> <!--configuration transaction manager--> <bean id= "TransactionManager" class = "Org.springframework.jdbc.datasource.DataSourceTransactionManager" > <!--inject database connection pool--> <proper Ty name= "DataSource" ref= "dataSource "/> </bean> <!--Configure annotations-based declarative transactions--> <tx:annotation-driven transaction-manager=" transact Ionmanager "/> </beans>

Web.xml

<web-app xmlns= "Http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemalocation= "Http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    version= "3.1" > <display-name>archetype Created Web application</display-name> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class> Org.springframework.web.servlet.dispatcherservlet</servlet-class> <!--Configure SPRINGMVC configuration files that need to be loaded Spring-da
      O.xml,spring-service.xml,spring-mvc.xml MyBatis-> Spring-> springmvc--> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/spring-*.xml< /param-value> </init-param> </servlet> <servlet-mapping> &LT;SERVLET-NAME&GT;DISPATCHER&L T;/servlet-name> <!--default to match all requests--> <url-pattern>*.do</url-pattern> </servlet-mapping> <filter> <filter-name>encodingfilter</filt er-name> <filter-class> Org.springframework.web.filter.CharacterEncodingFilter </filter-class&gt
    ;
    <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingfilter</filter-name&
    Gt <url-pattern>*.do</url-pattern> </filter-mapping> </web-app>

Java

User

Package Com.mcd.pojo;

Import java.io.Serializable;

public class User implements serializable{
    private int id;
    private String name;
    Private String pwd;

    public int getId () {return
        ID;
    }

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

    Public String GetName () {return
        name;
    }

    public void SetName (String name) {
        this.name = name;
    }

    Public String getpwd () {return
        pwd;
    }

    public void SetPwd (String pwd) {
        this.pwd = pwd;
    }

    @Override public
    String toString () {return
        "user{" +
                "id=" + ID +
                ", name= ' + name + ' \ ' +
                ", Pwd= ' + pwd + ' \ ' +
                '} ';
    }

Userdao

Package Com.mcd.dao;

Import Com.mcd.pojo.User;
Import org.springframework.stereotype.Repository;

Import java.util.List;

@Repository Public
interface Userdao {
    list<user> queryall ();
}
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.mcd.dao.UserDao" >

    <select id= "Queryall" resulttype= "User" >
        SELECT * from USER
    </select>
</mapper>

UserService

Package com.mcd.service;

Import Com.mcd.pojo.User;

Import java.util.List;

Public interface UserService {
    list<user> queryall ();
}

Userserviceimpl

Package Com.mcd.service.impl;

Import Com.mcd.dao.UserDao;
Import Com.mcd.pojo.User;
Import Com.mcd.service.UserService;
Import org.springframework.beans.factory.annotation.Autowired;
Import Org.springframework.stereotype.Service;

Import java.util.List;

@Service public
class Userserviceimpl implements userservice{
    @Autowired
    private Userdao Userdao;
    
    @Override public
    list<user> Queryall () {return
        userdao.queryall ();
    }
}

Usercontroller

Package Com.mcd.controller;

Import Com.mcd.service.UserService;
Import org.springframework.beans.factory.annotation.Autowired;
Import Org.springframework.stereotype.Controller;
Import Org.springframework.ui.Model;
Import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping ("/user") public
class Usercontroller {
    @Autowired
    private UserService UserService;

    @RequestMapping ("/list")
    private String list (model model) {
        Model.addattribute ("list"), Userservice.queryall ());
        return "list";
    }

If you have any questions, you can add me qq:834739007

SOURCE Link: https://download.csdn.net/download/qq_35062384/10370269



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.