https://blog.csdn.net/w8897282/article/details/71173211
The above are the two blog posts I found better to build a framework! The following is a detailed introduction to the process of setting up the SSM framework
Install and configure JDK1.8, maven3.5.2, etc
Just configure the maven- > conf ->settings. XML image. I usually use Ali’s image (fast after all!!). The diagram below:
The skeleton I built is as follows:
1, en route com.chatRobot
Uncheck Hide Empty Middle Packages, wait until the “class” under the directory such as Controller is built, and then select it.
2. Blue JavaAnd greenAs well asTo do this, configure File –> Project Structure –> Modules –> select what you want and then select the corresponding type (Sources, Tests, Resources, etc) as follows:
4,Just create the XML and change the type, as shown below:
5,This isGreen leafy XMLThe file will not appear until spring dependencies are introduced
Configuration of pom. The XML
The < 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” >
The < modelVersion > 4.0.0 < / modelVersion > < groupId > com. ChatRobot < / groupId > < artifactId > chatRobot < / artifactId >
1.0-snapshot
ChatRobot Webapp
http://maven.apache.org
4.3.5.RELEASE
3.4.1
<dependencies>
Javax
Javaee – API
7.0
junit
4.12
ch.qos. Logback
logback
< the dependency > < groupId > com. Fasterxml. Jackson. Core < / groupId > < artifactId > Jackson – databind < / artifactId > The < version > 2.8.7 < / version > < / dependency >
mysql
mysql-connector- Java
5.1.41
runtime
c3P0
0.9.5.2
org.mybatis
mybatis
${mybatis.version}
Mybatis
org.springframework
spring-core
${spring.version}
org.springframework
spring-beans
${spring.version}
org.springframework
spring-context
${spring.version}
org.springframework
spring-jdbc
${spring.version}
org.springframework
spring-tx
${spring.version}
org.springframework
spring-web
${spring.version}
org.springframework
spring-webmvc
${spring.version}
org.springframework
spring-test
${spring.version}
</dependencies>
ChatRobot
org.apache.maven.plugins
maven-compiler-plugin
</project>
Note that the poM is updated in the lower right corner
logback.xml
Here you can control the output format and content, you can set your own interest
jdbc.properties
JDBC. Driver = com. Mysql. JDBC. Address the JDBC driver # database. Url = JDBC: mysql: / / XXXXXXXXX: 3306 / ChatRobot? UseUnicode =true&characterEncoding=utf8 # username jdbc.username= XXXX # password jdbc.password= XXXXX # maximum connection c3p0.maxpoolsize =30 # Minimum connection number C3p0.checkouttimeout =10000 c3P0.checkoutTimeout =10000 c3p0.acquireRetryAttempts=2
spring-mybatis.xml
< context: the property – placeholder location = “classpath: JDBC. Properties” / >
< bean id = “dataSource” class = “boPooledDataSource com.mchange.v2.c3p0.Com” > < property name = “driverClass” value=”${jdbc.driver}”/>
< bean id = “SqlSessionFactory” class = “org. Mybatis. Spring. SqlSessionFactoryBean” >
< bean class = “org. Mybatis. Spring. Mapper. MapperScannerConfigurer” >
< property name = “sqlSessionFactoryBeanName” value = “sqlSessionFactory” / >
< bean id = “transactionManager” class = “. Org. Springframework. JDBC datasource. DataSourceTransactionManager “>
</beans>
spring-mvc.xml
< MVC :default-servlet-handler/>
< bean class = “org. Springframework. Web. Servlet. The InternalResourceViewResolver” > < property name=”viewClass” value=”org.springframework.web.servlet.view.JstlView”/>
</beans>
web.xml
< the display – name > ChatRobot < / display – name > < description > ChatRobot_Alpha_0. 0.1 < / description >
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encodingFilter
/*
< servletname >SpringMVC
org.springframework.web.servlet.DispatcherServlet
1
true
SpringMVC
/
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
Now that the environment is basically set up, let’s start testing
Four, test,
Import a database test file first
CREATE TABLE ‘user’ (‘ id ‘int(11) NOT NULL AUTO_INCREMENT COMMENT’ user id ‘, ’email’ varchar(255) NOT NULL COMMENT ‘username ‘,’ password ‘varchar(255) NOT NULL COMMENT’ username ‘, ‘username’ varchar(255) NOT NULL COMMENT ‘iD ‘,’ role ‘varchar(255) NOT NULL COMMENT’ id ‘, ‘status’ int(1) NOT NULL COMMENT’ iD ‘, ‘regTime’ datetime NOT NULL COMMENT ‘iD ‘, ‘regIp’ varchar(255) NOT NULL COMMENT ‘register IP’, PRIMARY KEY (‘ id’), UNIQUE KEY `email` (`email`) USING BTREE ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8; INSERT INTO ` user ` VALUES (‘ 1 ‘, ‘XXX’, ‘XXXXX’, ‘XXXXX’, ‘root’, ‘0’, ‘2017-03-28 09:40:31’, ‘127.0.0.1); SET FOREIGN_KEY_CHECKS=1;
Next configure the class
UserController
package com.chatRobot.controller;
import javax.servlet.http.HttpServletRequest;
import com.chatRobot.model.User;
import com.chatRobot.service.IUserService;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Controller
@RequestMapping(“/user”)
public class UserController {
@Resource
private IUserService userService;
@RequestMapping(“/showUser.do”)
public void selectUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
request.setCharacterEncoding(“UTF-8”);
response.setCharacterEncoding(“UTF-8”);
long userId = Long.parseLong(request.getParameter(“id”));
User user = this.userService.selectUser(userId);
ObjectMapper mapper = new ObjectMapper();
response.getWriter().write(mapper.writeValueAsString(user));
response.getWriter().close();
}
}
IUserDao
package com.chatRobot.dao;
import com.chatRobot.model.User;
public interface IUserDao {
User selectUser(long id);
}
User
package com.chatRobot.model;
import java.util.Date;
public class User {
private long id;
private String email;
private String password;
private String username;
private String role;
private int status;
private Date regTime;
private String regIp;
public long getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public Date getRegTime() {
return regTime;
}
public void setRegTime(Date regTime) {
this.regTime = regTime;
}
public String getRegIp() {
return regIp;
}
public void setRegIp(String regIp) {
this.regIp = regIp;
}
}
IUserService
package com.chatRobot.service;
import com.chatRobot.model.User;
public interface IUserService {
public User selectUser(long userId);
}
UserServiceImpl
package com.chatRobot.service.impl;
import com.chatRobot.dao.IUserDao;
import com.chatRobot.model.User;
import com.chatRobot.service.IUserService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service(“userService”)
public class UserServiceImpl implements IUserService {
@Resource
private IUserDao userDao;
public User selectUser(long userId) {
return this.userDao.selectUser(userId);
}
}
UserDao.xml
< mapper namespace = “com. ChatRobot. Dao. IUserDao” >
<select id=”selectUser” resultType=”User” parameterType=”long”>
SELECT * FROM user WHERE id = #{id}
</select>
</mapper>
Create a new test class to test mybatis
IUserDaoTest
package com.chatRobot.dao;
import com.chatRobot.model.User;
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.SpringJUnit4ClassRunner;
/ / load the spring configuration file @ RunWith (SpringJUnit4ClassRunner. Class) @ ContextConfiguration ({} “classpath: spring – mybatis. XML”) to the public class IUserDaoTest {
@Autowired
private IUserDao dao;
@Test
public void testSelectUser() throws Exception {
long id = 1;
User user = dao.selectUser(id);
System.out.println(user.getUsername());
}
}
The result should be the console output user name with id 1, if not successful, find the BUG…
Continue to create a new page to test SpringMVC and MyBatis
index.html
Hello World!
Configure the project runtime environment after creating it, click run-Edit Configurations…
Click the plus sign to create a new operating environment and select Tomcat server-local
Next select Tomcat –>Deployment
Just run the project