1. First use idea to create a new Maven webapp project
Click Finish, the first build may be slow, and may even require a VPN to build successfully
Second, build the directory structure
What I list here is all the directories and files after the construction is completed. First, build the directory files, and then I will give the contents of the files.
After the directory here is built, you need to set it up, let idea recognize the role of the directory, and select File-Project Structure
OK after setting
3. Contents of the configuration file
pom.xml
<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"> <modelVersion>4.0.0</modelVersion> <groupId>com.test</groupId> <artifactId>ssm01</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>ssm01 Maven Webapp</name> <url>http://maven.apache.org</url> <properties> <!-- Set the project code code--> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <!-- spring version number--> <spring.version>4.3.5.RELEASE</spring.version> <!-- mybatis version number--> <mybatis.version>3.4.1</mybatis.version> </properties> <dependencies> <!-- java ee --> <dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>7.0</version> </dependency> <!-- unit test --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <!-- Implement slf4j interface and integrate --> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.2</version> </dependency> <!-- JSON --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.8.7</version> </dependency> <!-- database --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.41</version> <scope>runtime</scope> </dependency> <!-- Database connection pool --> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.2</version> </dependency> <!-- MyBatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <!-- mybatis/spring integration package--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.1</version> </dependency> <!-- Spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> </dependencies> <build> <finalName>ssm01</finalName> </build> </project>
Note the update pom in the lower right corner
logback.xml
<?xml version="1.0" encoding="UTF-8"?> <configuration debug="true"> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> </encoder> </appender> <root level="debug"> <appender-ref ref="STDOUT"/> </root> </configuration>
Here you can control the output format and content, and you can set it yourself if you are interested
jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver #Database address jdbc.url=jdbc:mysql://xxxxxxxxx:3306/ChatRobot?useUnicode=true&characterEncoding=utf8 #username jdbc.username=xxxx #password jdbc.password=xxxxx #Maximum number of connections c3p0.maxPoolSize=30 #Minimum number of connections c3p0.minPoolSize=10 #Do not automatically commit after closing the connection c3p0.autoCommitOnClose=false #Get the connection timeout c3p0.checkoutTimeout=10000 #When the connection fails to get the number of retries c3p0.acquireRetryAttempts=2
spring-mybatis.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- Scan all types of annotations under the service package--> <context:component-scan base-package="com.test.service"/> <!-- Configure the properties of database related parameters properties: ${url} --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- Database connection pool --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driver}"/> <property name="jdbcUrl" value="${jdbc.url}"/> <property name="user" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> <property name="maxPoolSize" value="${c3p0.maxPoolSize}"/> <property name="minPoolSize" value="${c3p0.minPoolSize}"/> <property name="autoCommitOnClose" value="${c3p0.autoCommitOnClose}"/> <property name="checkoutTimeout" value="${c3p0.checkoutTimeout}"/> <property name="acquireRetryAttempts" value="${c3p0.acquireRetryAttempts}"/> </bean> <!-- Configure the SqlSessionFactory object --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- Inject database connection pool--> <property name="dataSource" ref="dataSource"/> <!-- Scan the model package using aliases --> <property name="typeAliasesPackage" value="com.test.model"/> <!-- Scan sql configuration file: xml file required by mapper--> <property name="mapperLocations" value="classpath:mapper/*.xml"/> </bean> <!-- Configure and scan the Dao interface package, dynamically implement the Dao interface, and inject it into the spring container --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- Inject sqlSessionFactory --> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> <!-- Give the Dao interface package that needs to be scanned --> <property name="basePackage" value="com.test.dao"/> </bean> <!-- Configure Transaction Manager --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- Inject database connection pool--> <property name="dataSource" ref="dataSource"/> </bean> <!-- Configure annotation-based declarative transactions --> <tx:annotation-driven transaction-manager="transactionManager"/> </beans>
spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!-- Scan web related beans --> <context:component-scan base-package="com.test.controller"/> <!-- Enable SpringMVC annotation mode--> <mvc:annotation-driven/> <!-- Static resource default servlet configuration--> <mvc:default-servlet-handler/> <!-- Configure jsp to display ViewResolver --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="prefix" value="/WEB-INF/views/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <display-name>ssm01</display-name> <description>ChatRobot_Alpha_0.0.1</description> <!-- encoding filter --> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 配置DispatcherServlet --> <servlet> <servlet-name>SpringMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- Configure the configuration file that springMVC needs to load--> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-*.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> <async-supported>true</async-supported> </servlet> <servlet-mapping> <servlet-name>SpringMVC</servlet-name> <!-- Match all requests, here can also be configured in the form of *.do --> <url-pattern>/</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> </web-app>
User's model
public class User { private int id; private String username; private String password; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
userController
@Controller @RequestMapping(value = "/test") public class UserController { @Resource private IUserService userService; @RequestMapping(value = "/save") public void saveUser() { User user=new User(); user.setUsername("Zhang San"); user.setPassword("852456"); userService.insertUser(user); } }
IUserService
public interface IUserService { /** * increase * @param user * @return */ public int insertUser(User user); /** * delete */ public int deleteUser(int id); /** * change */ public int updataUser(User user); /** * check */ public User selectUserById(int id); /** * find all */ public List selectALluser(); }
IUserServiceImpl
@Service("userService") public class UserServiceImpl implements IUserService { @Resource private IUserDao userDao; public int insertUser(User user) { userDao.insertUser(user); return 0; } public int deleteUser(int id) { return 0; } public int updataUser(User user) { return 0; } public User selectUserById(int id) { return null; } public List selectALluser() { return null; } }
IUserDao
public interface IUserDao { /** *Add user * @param user * @return */ int insertUser(User user); /** * delete users * @param id * @return */ int delUser (int id); /** * Modify user * @param user * @return */ int updateUser(User user); /** * Check user by id * @return */ User selectUserById(int id); /** * find all users * @return */ List<User> selectAllUser(); }
UserDao.xml
<?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.test.dao.IUserDao"><!-- The name of the corresponding dao layer interface--> <!-- Custom return result set--> <resultMap id="userMap" type="UserBean"> <id property="id" column="id" javaType="java.lang.Integer"></id> <result property="username" column="username" javaType="java.lang.String"></result> <result property="password" column="password" javaType="java.lang.String"></result> </resultMap> <!-- The id attribute in various tags must be the same as the method name in the interface, and the value of the id attribute must be unique and cannot be reused. The parameterType attribute indicates the parameter type used in the query, The resultType attribute indicates the type of result set returned by the query --> <!-- useGeneratedKeys: (useful for insert only) This tells MyBatis to use JDBC's getGeneratedKeys method to retrieve the primary key internally generated by the data (eg: auto-incrementing fields of database management systems like MySQL and SQLServer). Default: false. Oracle does not support it and should be set to useGeneratedKeys="false" otherwise an error will be reported --> <!--keyProperty: (only useful for insert) Marks a property, MyBatis will set its value through getGeneratedKeys or through the selectKey sub-element of the insert statement. Default: not set. --> The content in <!--#{} is a placeholder. When the parameter is a JavaBean, it indicates the attribute value of the Bean object --> <insert id="insertUser" useGeneratedKeys="true" keyProperty="id"> INSERT INTO t_user (username,password) VALUES (#{username},#{password}) </insert> <delete id="deleteUser" parameterType="int"> DELETE FROM t_user WHERE id=#{id} </delete> <update id="updateUser" parameterType="int"> UPDATE t_user SET username=#{username},password=#{password} WHERE id=#{id} </update> <select id="selectUserById" > SELECT * FROM t_user WHERE id=#{id} </select> <select id="selectAllUser"> SELECT * FROM t_user </select> </mapper>
https://www.cnblogs.com/hackyo/p/6646051.html
Reference article https://www.cnblogs.com/kevin1990/p/6231122.html