Spring-ssm integration details
I'll write down all the steps. A bad pen is better than a good memory. It doesn't feel anything just by looking at it. Let's do it manually. cheer up
1. Create projects
Projects are created in maven's most basic way
Modify to a Web project [pom.xml]
packaging
! packaging War /packagingCopy the code
Project code
! - set the encoding - properties project. Build. SourceEncoding utf-8 / project. Build. SourceEncoding / properties Copy the code
Dependent libraries
It depends on the situation
! -- dependencies ! -- Servlet + JSP -- dependency groupIdjavax.servlet/groupId artifactIdjavax.servlet-api/artifactId version4.0.1/version scope Provided /scope /dependency dependency groupIdjavax.servlet.jsp/groupId artifactIdjavax.servlet.jsp- API /artifactId version2.3.3/version scopeprovided/scope /dependency ! -- Log -- dependency groupIdch.qos.logback/groupId artifactIdlogback-classic/artifactId The version 1.2.3 / version / dependency ! -- Spring + SpringMVC -- dependency groupIdorg.springframework/groupId artifactIdspring-webmvc/artifactId version 5.2.8. RELEASE / version / dependency the dependency groupId org. Aspectj / groupId artifactId aspectjweaver / artifactId version 1.9.6 / version / dependency the dependency groupIdorg.aspectj/groupId artifactId aspectJrt /artifactId version1.9.6/version /dependency dependency the groupId com. Fasterxml. Jackson. Core / groupId artifactId Jackson - databind / artifactId version 2.11.0 / version /dependency ! -- MyBatis -- dependency groupIdorg. MyBatis /artifactId version3.5.5/version /dependency dependency groupIdorg.mybatis/groupId artifactIdmybatis-spring/artifactId version at 2.0.5 / version / dependency the dependency groupId org. Springframework / groupId artifactId spring - JDBC/artifactId version 5.2.8. RELEASE / version / dependency the dependency the groupId mysql / groupId artifactId mysql connector - Java / artifactId version 5.1.49 / version / dependency dependency groupIdcom.alibaba/groupId artifactIddruid/artifactId version1.1.22/version /dependency ! -- dependency groupId artifactIdjunit/artifactId version4.13/version scopetest/scope /dependency /dependenciesCopy the code
Project structure core configuration file [web.xml]
War is already a Web project because of the packaging method configured in POM.xml
Press the shortcut key to enter the project configuration
The initial interface
Add web.xml (change path oh)
src/main/webapp
Create a directory
The effect
2. Start the web
Create index. The JSP
Create index.jsp under webApp
Configuration tomact
Configure the Application Context
Enable bug debug
3. Create a service
Create a domain
Skill class
private Integer id;
private Date createdTime;
private String name;
private Integer level;
Copy the code
Create a dao layer
Create interface interface because use mybatis
boolean save(Skill skill);
boolean update(Skill skill);
boolean remove(Integer id);
@Select("SELECT * FROM skill")
ListSkill list();
@Select("SELECT * FROM skill WHERE id = #{id}")
Skill get(Integer id);
Copy the code
Create the mappers folder under Resources to create the file skill.xml
? The 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.mj.dao.SkillDao" insert id="save" parameterType="com.mj.domain.Skill" INSERT INTO skill(name, level) VALUES (#{name}, #{level}) /insert update id="update" parameterType="com.mj.domain.Skill" UPDATE skill SET name = #{name}, level = #{level} WHERE id = #{id} /update delete id="remove" parameterType="int" DELETE FROM skill WHERE id = #{id} /delete /mapperCopy the code
Create the applicationContext. XML
The mybatis SqlSessionFatoryBean
- Introduces a configuration file for database drivers
# jdbc
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/xr
jdbc.username=root
jdbc.password=root
Copy the code
- The applicationcontext.xml added
! - data source (Druid) -- - context: the property - placeholder location = "classpath: main. The properties" / bean id = "dataSource" class="com.alibaba.druid.pool.DruidDataSource" property name="driverClassName" value="${jdbc.driverClassName}"/ property name="url" value="${jdbc.url}"/ property name="username" value="${jdbc.username}"/ property name="password" value="${jdbc.password}"/ /bean bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" property name="dataSource" ref="dataSource"/ property name="mapperLocations" value="classpath:mappers/*.xml"/ /bean bean id="configurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer" property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/ property name="basePackage" value="com.mj.dao"/ /beanCopy the code
Test Tests a wave
4. Return json
Start with the service and Controller layers
Remember to inject it
Create a dispatcherServlet. The XML
Remember to scan the custom package in applicationContext.xml to add
context:component-scan base-package="com.mj.service"/
Copy the code
The dispatcherServlet. XML is added
context:component-scan base-package="com.mj.controller"/
Copy the code
Web.xml configures DispatcherServlet (parent-child container)
servlet servlet-nameDispatcherServlet/servlet-name ! / web-inf /${servlet-name}-servlet.xml-- servlet-classorg.springframework.web.servlet.DispatcherServlet/servlet-class ! init-param param-namecontextConfigLocation/param-name ! - children - param - value classpath: dispatcherServlet. XML value param - / init - param ! Create a servlet as soon as the project is deployed to the Web. -- load-on-startup0/load-on-startup /servlet servlet-mapping servlet-nameDispatcherServlet/servlet-name ! - intercept all requests - url - the pattern / / url - the pattern / servlet - mapping ! - the parent container - listener listener - class org. Springframework. Web. Context. ContextLoaderListener / listener - class / listener context-param param-namecontextConfigLocation/param-name param-valueclasspath:applicationContext.xml/param-value /context-paramCopy the code
The ContextLoaderListener is used to automatically assemble configuration information for Applicationcontenxt.xml when the Web container is started. Because it implements the ServletContextLister interface, when the web.xml listener is configured, the methods it implements are executed by default when the container is started.
Web. XML configures garbled POST requests
filter
filter-nameCharacterEncodingFilter/filter-name
filter-classorg.springframework.web.filter.CharacterEncodingFilter/filter-class
init-param
param-nameencoding/param-name
param-valueUTF-8/param-value
/init-param
/filter
filter-mapping
filter-nameCharacterEncodingFilter/filter-name
url-pattern/*/url-pattern
/filter-mapping
Copy the code
Dispatcherservlet. XML Configures garbled response data
mvc:annotation-driven
mvc:message-converters
bean class="org.springframework.http.converter.StringHttpMessageConverter"
property name="defaultCharset" value="UTF-8"/
/bean
bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"
property name="defaultCharset" value="UTF-8"/
/bean
/mvc:message-converters
/mvc:annotation-driven
Copy the code
MyBatis supplement
If the Mapper file is placed in the same directory as dao and the file name is the same as DAO
- You can find the Mapper file through MapperScannerConfigurer
- You no longer need to configure the mapperLocations property of SqlSessionFactoryBean
Solution: Keep the names the same
Create mybatis - config. XML
? The 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 settings setting name="mapUnderscoreToCamelCase" value="true"/ /settings /configurationCopy the code
Configuration configLocation
! -- property name="mapperLocations" value="classpath:mappers/*.xml"/-- property name="configLocation" value="classpath:mybatis-config.xml"/Copy the code
Maven Supplement (with Mabatis supplement)
By default, Maven does not package configuration files in the source folder so it does
- Problem with Dao layer XML in the same folder as Dao layer
build ! -- resources ! resource directory SRC /main/resources/directory /resource ! resource directory SRC /main/ Java /directory includes include**/*. Properties /include include**/*.xml/include /includes /resource /resources /buildCopy the code
To solve the problem
Attributes in XML report red
The general druid properties also have problems with the solution is
This does not solve the problem, I will create a new project can be and prompt is not the network problem