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

The method of Building Integrated Environment in SSM Framework

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Most people do not understand the knowledge points of this article "SSM Framework to build an integrated environment", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "SSM Framework to build an integrated environment" article.

1. Build an integrated environment

Integration description: SSM integration can be used in a variety of ways, we will choose XML + annotations

The idea of integration

2.1. Build an integrated environment first

2.2. First, complete the configuration of Spring.

2.3. Then use Spring to integrate the SpringMVC framework

2.4. Finally, Spring is used to integrate MyBatis framework.

Create database and table structures

3.1 create a database

Create database ssm;create table account (id int primary key auto_increment,name varchar (20), money double)

4. Create a maven project

5.0.2.RELEASE 1.6.6 1.2.12 5.1.6 3.4.5 org.aspectj aspectjweaver 1.6.8 org.springframework spring-aop ${spring.version} org.springframework spring-context ${spring.version} org.springframework spring-web ${spring.version} org.springframework spring-webmvc ${spring.version} org.springframework spring-test $ {spring.version} org.springframework spring-tx ${spring.version} org.springframework spring-jdbc ${spring.version} junit junit 4.12 test mysql mysql-connector-java ${mysql.version} javax.servlet servlet-api 2.5 provided javax.servlet.jsp jsp-api 2.0 provided jstl jstl 1.2 Log4j log4j ${log4j.version} org.slf4j slf4j-api ${slf4j.version} org.slf4j slf4j-log4j12 ${slf4j.version} org.mybatis mybatis ${mybatis.version} org.mybatis mybatis-spring 1.3.0 com.alibaba druid 1.1.10 ssm org.apache.maven.plugins maven-compiler-plugin 3.2 1.8 1.8 UTF-8 true

Write entity classes, in the ssm_domain project

Package com.qcby.entity;import java.io.Serializable;public class Account implements Serializable {/ / Primary key private int id; / / account name private String name; / / amount of account private Double money; 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 Double getMoney () {return money;} public void setMoney (Double money) {this.money = money } @ Override public String toString () {return "Account {" + "id=" + id + ", name='" + name +''+ ", money=" + money +'}';}}

Write service interfaces and implementation classes

Package com.qcby.service;import com.qcby.entity.Account;import java.util.List;public interface AccountService {/ / query all public List findAll ();} package com.qcby.service;import com.qcby.entity.Account;import org.springframework.stereotype.Service;import java.util.List;@Servicepublic class AccountServiceImpl implements AccountService {/ / query all @ Override public List findAll () {System.out.println ("Business layer: query all"); return null Chapter 2: the coding of Spring framework

1. Build and test the development environment for Spring

Create a configuration file for spring.xml in the ssm_web project and write specific configuration information.

Write test methods and test them in the ssm_web project

Package com.qcby.demo;import com.qcby.service.AccountService;import org.junit.Test;import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestSpring {@ Test public void run () {ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext ("classpath:spring.xml"); AccountService service = ac.getBean (AccountService.class); service.findAll ();} Chapter 3: Spring Integration SpringMVC Framework

1. Build and test the development environment for SpringMVC

Configure the DispatcherServlet front-end controller in web.xml

DispatcherServlet org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath:springmvc.xml 1 DispatcherServlet /

Configure DispatcherServlet filter in web.xml to solve Chinese garbled

CharacterEncodingFilter org.springframework.web.filter.CharacterEncodingFilter encoding UTF-8 CharacterEncodingFilter / *

Create a configuration file for springmvc.xml and write a configuration file

Test whether the framework of SpringMVC is successful.

4.1. Write index.jsp and list.jsp write, hyperlink

Title queries all

4.2. Create AccountController classes, write methods, and test

Package com.qcby.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.servlet.ModelAndView;@Controller@RequestMapping ("/ account") public class AccountController {/ * query all * @ return * / @ RequestMapping ("/ findAll") public ModelAndView findAll () {System.out.println ("presentation layer: query all"); ModelAndView mv = new ModelAndView () Mv.setViewName ("suc"); return mv;}} 2. Spring's framework for integrating SpringMVC

Objective: to successfully call the methods in the service object in controller.

When the project starts, load the configuration file of spring.xml and configure it in web.xml

ContextLoaderListener listener (this listener can only load the configuration text of applicationContext.xml in WEB-INF directory

So configure the global variable to load the configuration file under the classpath.

Archetype Created Web Application org.springframework.web.context.ContextLoaderListener contextConfigLocation classpath:spring.xml

Inject the service object into controller and call the method of the service object to test

Package com.qcby.controller;import com.qcby.entity.Account;import com.qcby.service.AccountService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.servlet.ModelAndView;import java.util.List;@Controller@RequestMapping ("/ account") public class AccountController {/ / dependency injection @ Autowired private AccountService accountService / * query all * @ return * / @ RequestMapping ("/ findAll") public ModelAndView findAll () {System.out.println ("presentation layer: query all"); / / call service's method List list = accountService.findAll (); ModelAndView mv = new ModelAndView (); mv.setViewName ("suc"); return mv Chapter 4: Spring Integration MyBatis Framework

1. Build and test the environment for MyBatis

Write the SqlMapConfig.xml configuration file and the core configuration file in the web project

AccountDao interface

Package com.qcby.dao;import com.qcby.entity.Account;import java.util.List;public interface AccountDao {/ / query all public List findAll ();}

Write AccountDao.xml

Select * from account

The method of writing tests

Package com.qcby.demo;import com.qcby.dao.AccountDao;import com.qcby.entity.Account;import org.apache.ibatis.io.Resources;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;import org.junit.Test;import java.io.IOException;import java.io.InputStream;import java.util.List;public class TestMybatis {@ Test public void run () throws IOException {InputStream in = Resources.getResourceAsStream ("mybatis.xml") SqlSessionFactory factory = new SqlSessionFactoryBuilder (). Build (in); SqlSession session = factory.openSession (); AccountDao mapper = session.getMapper (AccountDao.class); List list = mapper.findAll (); for (Account account: list) {System.out.println (account);} / / close resource session.close (); in.close () 2. Spring integrates MyBatis framework

Purpose: to configure the contents of the SqlMapConfig.xml configuration file into the applicationContext.xml configuration file

Inject a dao object into service for testing

The service code is as follows

Package com.qcby.service;import com.qcby.dao.AccountDao;import com.qcby.entity.Account;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import java.util.List;@Servicepublic class AccountServiceImpl implements AccountService {@ Autowired private AccountDao accountDao; / / query all @ Override public List findAll () {System.out.println ("Business layer: query all"); List list = accountDao.findAll (); return list }}

The controller code is as follows

Package com.qcby.controller;import com.qcby.entity.Account;import com.qcby.service.AccountService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.servlet.ModelAndView;import java.util.List;@Controller@RequestMapping ("/ account") public class AccountController {/ / dependency injection @ Autowired private AccountService accountService / * query all * @ return * / @ RequestMapping ("/ findAll") public ModelAndView findAll () {System.out.println ("presentation layer: query all"); / / call service's method List list = accountService.findAll (); for (Account account: list) {System.out.println (account) } ModelAndView mv = new ModelAndView (); mv.setViewName ("suc"); return mv;}}

Spring.xml configuration declarative transaction management

Form code

Title query all names: amount:

Controller code

Package com.qcby.controller;import com.qcby.entity.Account;import com.qcby.service.AccountService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.servlet.ModelAndView;import java.util.List;@Controller@RequestMapping ("/ account") public class AccountController {/ / dependency injection @ Autowired private AccountService accountService / * query all * @ return * / @ RequestMapping ("/ findAll") public ModelAndView findAll () {System.out.println ("presentation layer: query all"); / / call service's method List list = accountService.findAll (); for (Account account: list) {System.out.println (account) } ModelAndView mv = new ModelAndView (); mv.setViewName ("suc"); return mv;} @ RequestMapping ("/ save") public String save (Account account) {/ / Service save method accountService.save (account); return "suc";}}

Service interface and implementation class code

Package com.qcby.service;import com.qcby.entity.Account;import java.util.List;public interface AccountService {/ / query all public List findAll (); / / Save void save (Account account);} package com.qcby.service;import com.qcby.dao.AccountDao;import com.qcby.entity.Account;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import java.util.List;@Servicepublic class AccountServiceImpl implements AccountService {@ Autowired private AccountDao accountDao / / query all @ Override public List findAll () {System.out.println ("Business layer: query all"); List list = accountDao.findAll (); return list;} @ Override public void save (Account account) {accountDao.save (account);}}

Dao code

Package com.qcby.dao;import com.qcby.entity.Account;import java.util.List;public interface AccountDao {/ / query all public List findAll (); / / Save void save (Account account);}

Dao.xml code

Select * from account insert into account (name,money) values (# {name}, # {money}) the above is the content of this article on "how to build an integrated environment in SSM framework". I believe you all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about it, please follow the industry information channel.

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

Development

Wechat

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

12
Report