A SSM framework integrated and friendly article (a) __mybatis

Source: Internet
Author: User
Tags connection pooling

Reprint please indicate the source:
http://blog.csdn.net/forezp/article/details/53730333
This article comes from Fang Zhibong's blog

Recently, I was too busy to write the "Rxjava series" end, and two will be filled in mid-January, thank you. This article is about mybatis to build DAO layer, and this series of articles I read the Java High concurrent second kill series of the course, so the article used the example also from this series of courses. I. Environment/TOOLS

1. Development tool Idea

2. Database MySQL

3.javaweb Container Tomcat

4. Build Project Tools Maven

About these tools download installation, the environment is not introduced to build, Baidu. two. Build projects with Maven

The command architecture, of course, can also be built directly by the IDE.

Build command:

MVN Archetype:generate-dgroupid=org.forezp-dartifactid=minnkill-darchetypeartifactid=maven-archetype-webapp

Low version maven needs to change generate to create. three. Complement the SSM framework-dependent

Here are all the frameworks required by the SSM framework

<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/maven-v4_0_0.xsd" > <modelversion >4.0.0</modelVersion> <groupId>org.seckill</groupId> <artifactid>seckill</ artifactid> <packaging>war</packaging> <version>1.0.0</version> <name>seckill
      Maven webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <!--use junit4,4 above using annotations--> <groupId>junit</groupId> <artifactid>junit</artifactid > <version>4.11</version> <scope>test</scope> </dependency> <!--complete project 
      Dependent--> <!--1: Log, Java log: slf4j,log4j,logback,common-logging use: Slf4j+logback--> <dependency> <groupId>org.slf4j</groupId> <artifactid>slf4j-api</artifactid> <version>1.5.6</version> </dependency> <depe
      Ndency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupid>ch.qos.logback
    </groupId> <artifactId>logback-classic</artifactId> <version>1.1.1</version> </dependency> <!--2: Database related dependencies--> <dependency> <groupId>mysql</groupId> <ar Tifactid>mysql-connector-java</artifactid> <version>5.1.35</version> <scope>runtime& lt;/scope> </dependency> <dependency> <groupId>c3p0</groupId> <artifactid&
      gt;c3p0</artifactid> <version>0.9.1.2</version> </dependency> <dependency> <groupid>commons-dBcp</groupid> <artifactId>commons-dbcp</artifactId> <version>1.4</version> & lt;/dependency> <!--DAO Framework: MyBatis dependent--> <dependency> <groupid>org.mybatis</groupid&gt
      ; <artifactId>mybatis</artifactId> <version>3.3.0</version> </dependency> <!- -mybatis's own implementation of spring consolidation dependencies--> <dependency> <groupId>org.mybatis</groupId> <artifactid >mybatis-spring</artifactId> <version>1.2.3</version> </dependency> <!--3:serv Let Web dependent--> <dependency> <groupId>taglibs</groupId> <artifactId>standard< /artifactid> <version>1.1.2</version> </dependency> <dependency> <groupi D>jstl</groupid> <artifactId>jstl</artifactId> <version>1.2</version> ;/dependency>
    <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactid>jackson-da
      tabind</artifactid> <version>2.5.4</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <versio N>3.1.0</version> </dependency> <!--4:spring dependent--> <!--Spring core dependencies--> <depend
      Ency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.1.7.RELEASE</version> </dependency> <dependency> <groupid>org. Springframework</groupid> <artifactId>spring-beans</artifactId> <version>4.1.7.release </version> </dependency> <!--spring DAO layer dependencies--> <dependency> &LT;GROUPID&GT;ORG.SP Ringframework</groupid> <artifactId>spring-jdbc</artifactId> <version>4.1.7.RELEASE</version> </d ependency> <dependency> <groupId>org.springframework</groupId> <artifactid>spri Ng-tx</artifactid> <version>4.1.7.RELEASE</version> </dependency> <!--Spring Web phase Off-Dependency--> <dependency> <groupId>org.springframework</groupId> <artifactid>spring-w
      eb</artifactid> <version>4.1.7.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <
      Version>4.1.7.release</version> </dependency> <!--spring test dependent--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <v ersion>4.1.7.release&Lt;/version> </dependency> <!--introduces some tools--> <dependency> <groupid>commons-coll Ections</groupid> <artifactId>commons-collections</artifactId> <version>3.2</versio n> </dependency> </dependencies> <build> <finalName>seckill</finalName> &
 Lt;/build> </project>
four. Database Building

Imitation Java high concurrent seconds Kill series of video, built two tables, a sheet on inventory, a last second kill table, I directly paste the SQL statement, relatively simple.

--Create DATABASE Seckill;

-use database using Seckill; --Inventory Table CREATE table Seckill (' seckill_id ' bigint not NULL auto_increment COMMENT ' Commodity inventory id ', ' name ' VARCHAR () NOT NULL COM ment ' product name ', ' number ' int not null COMMENT ' inventory quantity ', ' start_time ' timestamp not null DEFAULT ' 2016-05-07 13:28:00 ', ' end_t IME ' timestamp not null default ' 2016-05-07 13:28:00 ', ' create_time ' timestamp not null default Current_timestamp commen T ' creation time ', PRIMARY key (seckill_id), key Idx_start_time (start_time), key Idx_end_time (end_time), Key Idx_create_time (CRE

ate_time)) Engine=innodb auto_increment=1000 DEFAULT Charset=utf8 comment= ' second kill inventory table '; ---Initialize data insert into Seckill (name,number,start_time,end_time) VALUES (' 1000 iphone6 ', 100, ' 2015-11-01 00:00:00 ', ' 2015-11-02 00:00:00 '), (' 500 Yuan seconds to kill ipad2 ', 200, ' 2015-11-01 00:00:00 ', ' 2015-11-02 00:00:00 '), (' 300 yuan seconds to kill Millet 4 ', 300, '

 2015-11-01 00:00:00 ', ' 2015-11-02 00:00:00 '), (' 200 yuan seconds kill red meter note ', 400, ' 2015-11-01 00:00:00 ', ' 2015-11-02 00:00:00 '); --Second kill success Schedule--User login authentication related information Create tabLe success_killed (' seckill_id ' bigint not null COMMENT ' second kill commodity id ', ' user_phone ' bigint not null COMMENT ' user mobile number ', ' state ' tinyint NOT null DEFAULT 0 COMMENT ' status indicator:-1: Invalid 0: Success 1: Paid ', ' create_time ' TIMESTAMP not null COMMENT ' creation time ', PRIMARY KEY (Seckill_id,user_phone),/* Joint primary key/Key Idx_create_time (create_time)) Engine=innodb DEFAULT Charset=utf8 comment= '
 Second kill schedule ';
Five. DAO layer entity Authoring and interface writing

1. Create org.forezp.entity packages, and write entities based on database fields.
The following is the Seckill inventory entity:

public class Seckill {
    private long seckillid;
    private String name;
    private int number;
    Private Date StartTime;
    Private Date Endtime;
    Private Date createtime;
    This eliminates the write Getter setter method, which is filled in on the actual project 
}
Create a Org.forezp.dao Package
Writing Seckill Interfaces
Public interface Seckilldao {
    //minus inventory
    int reducenumber (@Param ("Seckillid") long Seckillid, @Param ("Killtime") Date killtime);
    According to the ID query second kill object
    Seckill Querybyid (long seckillid);
    Query seconds according to offset list
    list<seckill> queryall (@Param ("offset") int offset, @Param ("limit") int limit);

Implement Interface with Mapper

Create the Maaper folder in the resources directory, where all the interface implementation classes are placed, and the other projects implement interfaces in XML. The benefits of this approach are many, such as the use of packet scanning, less configuration, all the SQL statements are placed here, in favor of uniform specifications, code maintainability and readability are greatly improved.

<?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" &G


T
        <mapper namespace= "Org.forezp.dao.SeckillDao" > <update id= "reducenumber" > Update Seckill Set number = number-1 where seckill_id = #{seckillid} and Start_time <!
    [cdata[<=]]> #{killtime} and End_time >= #{killtime} and number > 0; </update> <select id= "Querybyid" resulttype= "Seckill" parametertype= "Long" > select Seckill_id,na Me,number,start_time,end_time,create_time from Seckill where seckill_id = #{seckillid} </select> &L
        T;select id= "Queryall" resulttype= "Seckill" > select Seckill_id,name,number,start_time,end_time,create_time From Seckill to create_time desc limit #{offset},#{limit} </select> &LT;/mapper>
 
Six. Configuration of MyBatis

To create a mybatis-config.xml global configuration:

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


 <!--Configure global properties-->    

<settings>
        <!--using JDBC Getgeneratedkeys Gets the database's self-added primary key value-->
        <setting name= "Usegeneratedkeys" value= "true"/>
        <!--use aliases to replace column names default True
        Select name as title from table
        -->
        <setting name= "Usecolumnlabel" value= "true"/>
        <!-- Open hump named conversion-->
        <setting name= "Mapunderscoretocamelcase" value= "true"/>

    </settings>

</configuration>

Creating jdbc.properties Files
Database configuration file and connection database driver.

Driver=com.mysql.jdbc.driver
url=jdbc:mysql://localhost:3306/seckill?useunicode=true& Characterencoding=utf8
username=root
password=123
Seven. MyBatis and Spring Integration

Directly paste the configuration file, the file has been written notes.

<?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.xsd http://www.springframework.org/schema/context http:// Www.springframework.org/schema/context/spring-context.xsd "
>

    <!--configuration Integration MyBatis process-->

    <!-- 1. Metabase related parameters-->
    <context:property-placeholder location= "classpath:jdbc.properties"/>

      <!--2. Configure connection Pooling Properties-->   
    <bean id= "DataSource class=" Org.apache.commons.dbcp.BasicDataSource

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.