Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Hand-in-hand Maven to build SpringMVC+Spring+MyBatis framework (super detailed version) [transfer]

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

one。 Frame details

Spring is a lightweight Java development framework, which is created to solve the complexity of enterprise application development. The use of Spring is not limited to server-side development. Any Java application can benefit from Spring in terms of simplicity, testability, and loose coupling. To put it simply, Spring is a lightweight inversion of control (IoC) and AOP-oriented container framework.

SpringMVC is a successor to SpringFrameWork, separating the roles of controllers, model objects, dispatchers, and handler objects, making them easier to customize.

MyBatis is a persistence layer framework based on Java. The persistence layer framework provided by MyBatis includes SQL Maps and Data Access Objects (DAO), which eliminates almost all manual setting of JDBC code and parameters and retrieval of result sets. MyBatis uses simple XML or annotations for configuration and raw mapping, mapping interfaces and Java to records in the database.

two。 Create a Maven project

Create a project with Maven in Eclipse

By default Next

When you find maven-archetype-webapp, click next

Fill in the appropriate information. GroupID is the unique identifier of the project organization, which actually corresponds to the structure of the JAVA package. ArtifactID is the unique identifier of the project, and the actual name of the corresponding project is the name of the project root. Package filled in the default for you to build a package, you can not write.

The catalogue just built is as follows

Maven stipulates that the following Source Folder must be added:

Src/main/resources

Src/main/java

Src/test/resources

Src/test/java

Before this step, it is best to right-click properties on the project, then click java build path, under Librarys, edit JRE System Library, and select workspace default jre.

Modify the output path respectively, and the corresponding relationship is as follows:

Convert the project to Dynamic Web Project, right-click Properties on the project, and select Project Facets on the left.

Set the file release path at deployment time and delete the two items of test, because test is for test use and does not need to be deployed.

Set up to publish the jar package of Maven to lib. Add-> Java Build Path Entries-> Maven Dependencies-> Finish

three。 Maven introduces the required JAR package

Xml code

four。 Configuration of related profiles, integration of SSM framework

Web.xml

Java code

Spring.xml

Java code

Instead of manually coding data access objects (DAO) using SqlSessionDaoSupport or SqlSessionTemplate, MyBatis-Spring provides an implementation of a dynamic proxy: MapperFactoryBean. This class allows you to inject the data mapper interface directly into your service layer bean. When using mappers, you can just call them as if you were calling your DAO, but you don't need to write any code for the DAO implementation, because MyBatis-Spring will create proxies for you.

Spring-mybatis.xml

Java code

Spring-mvc.xml

Java code

Log4j.properties

Sql code

Jdbc.properties

Java code

five。 Using MyBatis Generator to automatically create entity classes, mapping files and DAO interfaces

MyBatis belongs to a semi-automatic ORM framework, so the main task is to configure the Mapping mapping file, but because the handwritten mapping file is easy to make mistakes, the MyBatis generator can be used to automatically generate entity classes, DAO interfaces and Mapping mapping files. This saves a lot of effort by copy the generated code into the project project.

Generate the files and jar needed for the code and establish the following directory structure:

Configure the relevant database connection in generatorl.xml, and already have the database table:

Xml code

Open a CMD window to enter the directory structure and enter the command line:

Java-jar mybatis-generator-core-1.3.2.jar-configfile generator.xml-overwrite

After the completion of the run, the corresponding dao mapper and model will be generated, which is not cool.

six。 Establish Service layer and conrorller layer

Java code

Java code

Java code

seven。 Create a test class

Java code

Import java.util.List

Import org.apache.log4j.LogManager

Import org.apache.log4j.Logger

Import org.junit.Test

Import org.junit.runner.RunWith

Import org.springframework.beans.factory.annotation.Autowired

Import org.springframework.test.context.ContextConfiguration

Import org.springframework.test.context.junit4.SpringJUnit4Cla***unner

@ RunWith (SpringJUnit4Cla***unner.class)

@ ContextConfiguration (locations = {"classpath:spring.xml"})

Public class TestMybatis {

@ Autowired

Private OtherServiceI service

@ Test

Public void test () {

String list=service.getOterList ()

Logger.info (list)

}

}

Package com.yingjun.test.controller

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.ResponseBody

Import com.alibaba.fastjson.JSON

Import com.yingjun.test.service.OtherServiceI

Import com.yingjun.test.service.StockStatusServiceI

@ Controller

@ RequestMapping (value = "/")

Public class TSSController {

@ Autowired

Private OtherServiceI otherService

@ RequestMapping (value= "/ getOtherList", produces= "text/html;charset=UTF-8")

@ ResponseBody

Private String getOtherList () {

String json=otherService.getOterList ()

Return json

}

}

Package com.yingjun.test.service

Import java.util.ArrayList

Import java.util.Arrays

Import java.util.HashSet

Import java.util.List

Import java.util.Map

Import java.util.Set

Import org.springframework.beans.factory.annotation.Autowired

Import org.springframework.stereotype.Service

Import com.alibaba.fastjson.JSON

Import com.alibaba.fastjson.serializer.SerializerFeature

Import com.yingjun.test.dao.OtherListMapper

Import com.yingjun.test.model.OtherList

Import com.yingjun.test.model.OtherListDomain

@ Service

Public class OtherServiceImpl implements OtherServiceI {

@ Autowired

Private OtherListMapper otherListMapper

@ Override

Public String getOterList () {

Set set=new HashSet ()

List list=otherListMapper.selectAll ()

List jsonList=new ArrayList ()

For (OtherList other:list) {

String title=other.getTitle ()

If (set.contains (title)) {

Continue

} else {

List t_list=new ArrayList ()

For (OtherList data:list) {

If (title.equals (data.getTitle () {

T_list.add (data)

}

}

OtherListDomain domain=new OtherListDomain ()

Domain.setTitle (title)

Domain.setItems (t_list)

JsonList.add (domain)

Set.add (other.getTitle ())

}

}

Return JSON.toJSONString (jsonList, SerializerFeature.WriteMapNullValue)

}

}

Package com.yingjun.test.service

Public interface OtherServiceI {

Public String getOterList ()

}

Log4j.rootLogger=info, console, debug, app, error

# Console #

Log4j.appender.console = org.apache.log4j.ConsoleAppender

Log4j.appender.console.Target = System.out

Log4j.appender.console.layout = org.apache.log4j.PatternLayout

Log4j.appender.console.layout.ConversionPattern =% d% p [% CVA% L] -% m% n

# debug #

Log4j.appender.debug = org.apache.log4j.DailyRollingFileAppender

Log4j.appender.debug.File = log/debug.log

Log4j.appender.debug.Append = true

Log4j.appender.debug.Threshold = DEBUG

Log4j.appender.debug.DatePattern='.'yyyy-MM-dd

Log4j.appender.debug.layout = org.apache.log4j.PatternLayout

Log4j.appender.debug.layout.ConversionPattern =% d% p [% CMV% L] -% m% n

# app #

Log4j.appender.app = org.apache.log4j.DailyRollingFileAppender

Log4j.appender.app.File = log/app.log

Log4j.appender.app.Append = true

Log4j.appender.app.Threshold = INFO

Log4j.appender.app.DatePattern='.'yyyy-MM-dd

Log4j.appender.app.layout = org.apache.log4j.PatternLayout

Log4j.appender.app.layout.ConversionPattern =% d% p [% CMV% L] -% m% n

# Error #

Log4j.appender.error = org.apache.log4j.DailyRollingFileAppender

Log4j.appender.error.File = log/error.log

Log4j.appender.error.Append = true

Log4j.appender.error.Threshold = ERROR

Log4j.appender.error.DatePattern='.'yyyy-MM-dd

Log4j.appender.error.layout = org.apache.log4j.PatternLayout

Log4j.appender.error.layout.ConversionPattern =% d% p [% CMV% L] -% m% n

Jdbc.driverClassName=com.mysql.jdbc.Driver

Jdbc.url=jdbc:mysql://192.168.1.194:3306/test?useUnicode=true&characterEncoding=UTF-8

Jdbc.username=root

Jdbc.password=root

C3p0.pool.size.max=20

C3p0.pool.size.min=5

C3p0.pool.size.ini=3

C3p0.pool.size.increment=2

Text/html;charset=UTF-8

ContextConfigLocation

Classpath:spring.xml

Org.springframework.web.context.ContextLoaderListener

Org.springframework.web.util.IntrospectorCleanupListener

SpringMVC

Org.springframework.web.servlet.DispatcherServlet

ContextConfigLocation

Classpath:spring-mvc.xml

one

SpringMVC

/

EncodingFilter

Org.springframework.web.filter.CharacterEncodingFilter

Encoding

UTF-8

ForceEncoding

True

EncodingFilter

/ *

4.0.0

Com.yingjun.test

TradingState

War

2.0.1

UTF-8

YyyyMMddHHmmss

3.2.9.RELEASE

3.1.1

1.1.1

Org.springframework

Spring-core

${spring.version}

Org.springframework

Spring-webmvc

${spring.version}

Org.springframework

Spring-test

${spring.version}

Org.mybatis

Mybatis

${mybatis.version}

Org.mybatis

Mybatis-spring

${mybatisspring.version}

Mysql

Mysql-connector-java

5.1.34

Junit

Junit

4.11

Test

C3p0

C3p0

0.9.1.2

Org.aspectj

Aspectjweaver

1.8.1

Javax.servlet

Jstl

1.2

Javax.servlet

Servlet-api

3.0

Provided

Javax.servlet.jsp

Jsp-api

2.2

Provided

Commons-fileupload

Commons-fileupload

1.3.1

Commons-lang

Commons-lang

2.6

Commons-codec

Commons-codec

1.9

Org.apache.httpcomponents

Httpclient

4.5

Org.slf4j

Slf4j-api

1.7.10

Org.slf4j

Slf4j-log4j12

1.7.10

Log4j

Log4j

1.2.17

Com.alibaba

Fastjson

1.1.41

Org.codehaus.jackson

Jackson-mapper-asl

1.9.13

Maven-compiler-plugin

2.3.2

1.7

1.7

Maven-war-plugin

2.2

3.0

False

${project.artifactId} _ ${project.version} _ ${maven.build.timestamp}

Framework / platform composition:

Maven+Springmvc + Mybatis + Shiro (permission) + Tiles (template) + ActiveMQ (message queue) + Rest (Service) + WebService (Service) + EHcache (Cache) + Quartz (scheduled scheduling) + Html5 (support for PC, IOS, Android)

User permissions system:

Organizational structure: roles, users, user groups, organizations; authority points: pages, methods, buttons, data permissions, hierarchical authorization

New experience in project management:

Rapid prototyping system, component tree, version control, module migration, collaborative development, real-time monitoring, release management

Sustainable integration:

All components are portable, customizable and expandable, and the development achievements continue to accumulate, forming a virtuous circle of sustainable development.

Support platform platform:

Windows XP 、 Windows 7 、 Windows 10 、 Linux 、 Unix

Server container:

Tomcat 5-6-7 、 Jetty 、 JBoss 、 WebSphere 8.5

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report