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

Spring mvc configuration + dbcp data source + jdbcTemplate

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

Share

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

Recently, I intend to study spring carefully, starting with spring mvc, which has been used for 2 years. Beginners can take a look at it. The great god will be fine with pass, hehe.

First, go to the spring official website to download the complete spring package. The version containing libs, docs and schema,spring is 3.2.4.

Let's take a look at what spring's lib package contains:

In the above picture, except for the two jar in the red box, the other are the official jar packages provided by spring. The jar in the red box will be used when configuring the transaction. We will talk about it later. If we take a closer look at the jar package provided by spring, we can see that each module corresponds to 3 jar packages, sources packages (source code), javadoc packages (documentation), and compiled jar.

Then let's take a look at the modules. Let's first take a look at an overview diagram provided by the spring document to see if these jar correspond to its modules.

The first module, data storage / integration, which in turn includes JDBC,ORM (object-relational mapping), OXM (object/xml mapping), JMS (java messaging service), Transactions (transaction)

The second module, Web (MVC/Remoting), contains the web layer, web-servlet (including the implementation of spring mvc), and web-portlet,web-struts

The third module, AOP (aspect-orented programming), is what we usually call aspect-oriented programming.

The fourth module, Aspects, provides integration with AspectJ

The fifth module, Instrumentation, provides support for class instrumentation and class loader implementation.

The sixth module, Core Container (core container), also contains Beans and Core (which provides the basic parts of the framework, including control inversion and dependency injection features) and Context,Expression Language (which provides a powerful expression language for querying and manipulating object graphs at run time).

The seventh module, Test, provides the ability to test spring components, which is said to be quite powerful,

Driven project construction

Built-in efficient and reliable code generator

Support a variety of data models, generate routine repetitive code according to database tables, make R & D engineers focus more on the implementation of business logic code, greatly improve their work efficiency and liberate their repetitive work.

Take a look at the jar should all correspond. Let's go on.

Create a new web project in myeclipse and create the relevant directory structure, as shown below:

The config folder is a sources folder used to place configuration files.

Put the jar package of spring in the lib directory. Jar can choose according to the function you want to use. If you are lazy or do not want to use the function in the future, put it together. Be careful not to put the document jar and source code jar.

I suddenly remembered some requirements of spring for the environment, and forgot to mention that my jdk version is 1.6, the database will use mysql, and the application server is Tomcat7.0.

All right, now for springmvc configuration, we all know that the configuration file for spring is called applicationContext.xml, and the configuration file for springmvc is called springmvc.xml. In fact, as long as these two files are written into one, we will name them springmvc.xml.

To create a springmvc.xml file in the config directory, let's configure the sprigmvc.xml file first

Configure the spring file header

The file header is mainly a reference to sping's .xsd file, personal opinion, does not guarantee accuracy, you can click on the link to have a look, for example: http://www.springframework.org/schema/mvc/

You can see the various versions of the spring-mvc*.xsd file provided by spring

Then you remember to quote whatever you use. I quote everything. There is aop,context,tx,mvc.....

Next, add annotation support:

Automatically scan the spring component, which can be configured to the project root directory, such as com.xg.myspring. If you want to know more, please see the more detailed code.

Configure the parsing of the view, that is, the jump from the background to the page

When the default page jumps, the path looks for the file of * .jsp from the page/ directory

Do another exception handling configuration:

After an exception occurs, the program will jump to the specified error page to enhance the friendliness of the program.

A general configuration has been made here, because Exception is the parent of an exception, and whenever an exception occurs, it will jump to the error.jsp file in the error directory.

Error/error

Next, to configure the database, we use the dbcp type data source and add the mysql database connection jar,common-dbcp.jar in the lib directory. Remember to add common-logging.jar, which is used by spring logs by default.

Put the written database configuration file in the config directory

The jdbc.properties file is as follows:

Jdbc.driverClassName=com.mysql.jdbc.Driver

Jdbc.url=jdbc\: mysql\: / / 127.0.0.1\: 3306/report

Jdbc.username=root

Jdbc.password=12345

Here is the code to configure the data source:

Configuring a jdbcTemplate

That's all springmvc.xml can be configured for the time being, so let's configure the web.xml file.

Configure spring snooping for context first

Org.springframework.web.context.ContextLoaderListener

Configure the path to contextConfigLocation,spirngmvc.xml

ContextConfigLocation

Classpath:springmvc.xml

Configure spring dispatcher

Springmvc

Org.springframework.web.servlet.DispatcherServlet

ContextConfigLocation

Classpath:springmvc.xml

one

If you take a closer look, you will find that contextConfigLocation has been configured twice. Here * * / technology has source code.

Now the configuration work is basically completed, and the next step is to add the test code. We will add a control, a service, a dao, a login page, and a main page that prompts us to log in successfully according to the mvc mode.

Controller SystemUserControl.java

Package com.xg.myspring.control

Import java.io.File

Import java.io.FileInputStream

Import java.io.IOException

Import java.util.List

Import java.util.Properties

Import javax.annotation.Resource

Import javax.servlet.http.HttpServletRequest

Import javax.servlet.http.HttpServletResponse

Import org.springframework.stereotype.Controller

Import org.springframework.ui.Model

Import org.springframework.web.bind.annotation.PathVariable

Import org.springframework.web.bind.annotation.RequestMapping

Import org.springframework.web.bind.annotation.RequestMethod

Import org.springframework.web.servlet.ModelAndView

Import org.springframework.web.servlet.View

Import com.xg.myspring.entity.SystemUser

Import com.xg.myspring.service.SystemUserService

@ Controller

@ RequestMapping ("/ systemuser")

Public class SystemUserControl {

@ Resource

Private SystemUserService systemUserService

@ RequestMapping (value = "/ login", method = RequestMethod.GET)

Public ModelAndView login (SystemUser user) {

String message = "login succeeded!"

ModelAndView mv = new ModelAndView ("index")

Mv.addObject ("user", user)

Mv.addObject ("message", message)

Return mv

}

@ RequestMapping (value = "/ login", method = RequestMethod.POST)

Public String login2 (HttpServletRequest request, HttpServletResponse response, SystemUser user) {

Request.getSession () .setAttribute (user, user)

Request.getSession () .setAttribute ("message", "login succeeded!")

Return "redirect:/page/index.jsp"

}

@ RequestMapping ("/ queryList")

Public String queryList (HttpServletRequest request) {

List list = null

String sql = "select * from systemuserinfotable"

List = systemUserService.queryUserList (sql)

Request.setAttribute ("list", list)

Return "index"

}

}

The SystemUserServiceImpl.java interface is omitted.

Package com.xg.myspring.service.impl

Import java.util.List

Import javax.annotation.Resource

Import org.springframework.stereotype.Service

Import com.xg.myspring.dao.SystemUserDao

Import com.xg.myspring.entity.SystemUser

Import com.xg.myspring.service.SystemUserService

@ Service

Public class SystemUserServiceImpl implements SystemUserService {

@ Resource

Private SystemUserDao systemUserDao

Public void addSystemUser (SystemUser systemUser) {

SystemUserDao.addSystemUser (systemUser)

}

Public void deleteSystemUser (String sql) {

SystemUserDao.deleteSystemUser (sql)

}

Public SystemUser getSystemUserById (String sql) {

Return systemUserDao.getSystemUserById (sql)

}

Public List queryUserList (String sql) {

SystemUserDao.addSystemUser (new SystemUser ())

Return systemUserDao.queryUserList (sql)

}

}

SystemUserDaoImpl.java, the interface is omitted

Package com.xg.myspring.dao.impl

Import java.util.Date

Import java.util.List

Import javax.annotation.Resource

Import org.springframework.jdbc.core.BeanPropertyRowMapper

Import org.springframework.jdbc.core.JdbcTemplate

Import org.springframework.stereotype.Repository

Import com.xg.myspring.dao.SystemUserDao

Import com.xg.myspring.entity.SystemUser

@ Repository ("SystemUserDao")

Public class SystemUserDaoImpl implements SystemUserDao {

@ Resource

Private JdbcTemplate jdbcTemplate

Public void addSystemUser (SystemUser systemUser) {

Date date=new Date ()

String sql= "insert into systemuserinfotable values ('000 _" + date.getTime () + "', 'abc" + date.getTime () + ",' abc','1','1','test')"

If (systemUseroperations null) {

JdbcTemplate.execute (sql)

} else {

Throw new NullPointerException ()

}

}

Public void deleteSystemUser (String sql) {

}

Public List queryUserList (String sql) {

List list = jdbcTemplate.query (sql, new BeanPropertyRowMapper (SystemUser.class))

Return list

}

Public SystemUser getSystemUserById (String sql) {

Return jdbcTemplate.queryForObject (sql, new BeanPropertyRowMapper (SystemUser.class))

}

}

Classic introduction:

Source structure

Welcome to study and study related technologies, friends who are willing to understand the framework technology or source code to ask directly (Penguin): 2042849237

For more details, source code reference: http:// × × / technology

Now run it and report an exception of java.lang.ClassNotFoundException: org.apache.commons.pool.KeyedObjectPoolFactory. You can see that common-pool.jar is missing.

Add jstl.jar and servlet-api.jar together, and there will be no problem to run.

Landing

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