Simple sorting of SpringBoot integrated SSM environment configuration file version and annotation version in Eclipse based on Web
catalogue
3, SpringBoot integrates SSM environment and uses the method of configuration file
4, SpringBoot integrates SSM environment and uses annotation method
1, Brief introduction
Some knowledge of Web development is sorted out to facilitate the timely reference and use of similar problems in the later stage.
This section introduces the simple arrangement of SpringBoot integrated SSM environment configuration file version and annotation version in Eclipse in Web development. In fact, the main difference lies in the method of calling the database, and the need for configuration files is introduced c3p0; When the two integrated environment versions are put together, you can intuitively feel the difference between the two. You can select the appropriate version to use according to your needs. If there are deficiencies, you are welcome to point out, or you have a better method, you are welcome to leave a message.
2, Relevant environment
1,Windows 10
2,Eclipse 2021-06 (4.20.0)
3,apache-maven-3.8.3
4,MySQL 8.0.23 + MySQL Workbench 8.0 CE
3, SpringBoot integrates SSM environment and uses the method of configuration file
1. Open the website, create a maven project, and add Spring Web, Mybatis Framework and MySQL Driver
website: https://start.spring.io/
(Note: the spring boot version can be selected according to your needs, java can be selected according to your actual installation version, and others can be set and added according to your needs )
2. After setting up, download the project locally and unzip it
3. Open Eclipse, File - Import, and import the maven project
4. Wait a moment, then import and display
(if not, it may still be loading, or switching to the project state, or restarting Eclipse)
5. Open MySQL Workbench, create a new database and table, and add some data for simple testing
6. Add some package s under project src/main/java for simple classification management script
7. Add the User data model under the entity package to match the data items of the tables created by MySQL
package com.example.maven_srpingboot_ssm_config_file_test.entity; /* * Note: the class parameter corresponds to the database table * */ public class User { public Integer id; private String username; private String password; public Integer getId() { return id; } public void setId(Integer 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; } @Override public String toString() { return "User [id=" + id + ", username=" + username + ", password=" + password + "]"; } }
8. Create the interface script UserDao under dao package. Here is the interface script that can be connected to the database and operated accordingly (here, only a simple query interface is added for testing)
package com.example.maven_srpingboot_ssm_config_file_test.dao; import java.util.List; import com.example.maven_srpingboot_ssm_config_file_test.entity.User; public interface UserDao { // Check select * from user List<User> searchAllUsers(); }
9. Add a mapper folder under src/main/resource of the project, and add the xml files UserMapper and MybatisConfig
(UserMapper is used to map database statements (here simply configure the statements for querying data), MybatisConfig Configure some parameters)
UserMapper.xml (note that it corresponds to dao script)
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mubatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!-- namespace:Mapper Corresponding Dao --> <mapper namespace="com.example.maven_srpingboot_ssm_config_file_test.dao.UserDao"> <!-- select:query clause id: Corresponding method name resultType: Type returned --> <select id="searchAllUsers" resultType="com.example.maven_srpingboot_ssm_config_file_test.entity.User"> select * from user </select> </mapper>
MybatisConfig.xml
<?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> <!-- Get the value of the primary key auto increment --> <setting name="useGeneratedKeys" value="true"/> <!-- Turn on the transformation of hump naming --> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings> </configuration>
10. Introducing c3p0 into pom.xml
<?xml version="1.0" encoding="UTF-8"?> <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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.6</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>maven_srpingboot_ssm_config_file_test</artifactId> <version>0.0.1-SNAPSHOT</version> <name>maven_srpingboot_ssm_config_file_test</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.0</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- introduce c3p0 --> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.2</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
11. Add a script in the config package to configure the database and other related information. The key is to build various relationships with some data and key scripts (Note: replace some database related with your database related)
package com.example.maven_srpingboot_ssm_config_file_test.config; import java.beans.PropertyVetoException; import java.io.IOException; import javax.sql.DataSource; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.annotation.MapperScan; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import com.mchange.v2.c3p0.ComboPooledDataSource; @Configuration // mapper's scan path @MapperScan("com.example.maven_srpingboot_ssm_config_file_test.dao") public class SQLConfig { @Autowired private DataSource dataSource; // The name is aligned with @ Bean(name="dataSource") // dataSource @Bean(name="dataSource") public ComboPooledDataSource createDataSource() throws PropertyVetoException { ComboPooledDataSource dataSource = new ComboPooledDataSource(); // driver dataSource.setDriverClass("com.mysql.cj.jdbc.Driver"); // url dataSource.setJdbcUrl("jdbc:mysql://::1:3306/maven_ssm?serverTimezone=UTC&characterEncoding=utf-8&useSSL=true"); // username dataSource.setUser("root"); // password dataSource.setPassword("root123"); dataSource.setAutoCommitOnClose(false); return dataSource; } // SessionFactory @Bean(name="sqlSessionFactory") public SqlSessionFactoryBean createSqlSessionFactoryBean() throws IOException { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); // Load the main configuration file mybatis-config.xml bean.setConfigLocation(new ClassPathResource("MybatisConfig.xml")); // Mapper scan path PathMatchingResourcePatternResolver reslover = new PathMatchingResourcePatternResolver(); String packageSearchPath = "classpath*:/mapper/**.xml"; bean.setMapperLocations(reslover.getResources(packageSearchPath)); // Configure entity package bean.setTypeAliasesPackage("com.example.maven_srpingboot_ssm_config_file_test.entity"); // dataSource bean.setDataSource(dataSource); return bean; } }
12. Add a test script TestController in the controller package to test the configuration file method and call whether the database configuration is successful. Here, simply query the database information
package com.example.maven_srpingboot_ssm_config_file_test.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.example.maven_srpingboot_ssm_config_file_test.dao.UserDao; import com.example.maven_srpingboot_ssm_config_file_test.entity.User; @RestController public class TestController { @Autowired private UserDao mUserDao; @RequestMapping("/test") public String testSearchAllUsers() { List<User> users = mUserDao.searchAllUsers(); for(User user : users) { System.out.println(user.toString()); } return "testSearchAllUsers"; } }
13. As shown in the figure, select startup script, right-click Run As and run maven project
14. Enter localhost:8080/test on the web page, and the Console will print out the database information
15. Here we are SpringBoot integrates the SSM environment, and the method of using the configuration file is basically completed
4, SpringBoot integrates SSM environment and uses annotation method
1. Open the website, create a maven project, and add Spring Web, Mybatis Framework and MySQL Driver
website: https://start.spring.io/
(Note: the spring boot version can be selected according to your needs, java can be selected according to your actual installation version, and others can be set and added according to your needs )
2. After setting up, download the project locally and unzip it
3. Open Eclipse, File - Import, and import the maven project
4. Wait a moment, then import and display
(if not, it may still be loading, or switching to the project state, or restarting Eclipse)
5. Open MySQL Workbench, create a new database and table, and add some data for simple testing
(use the data from the previous case)
6. Add database information in application.properties in project src/main/resources
(note to change the file to UTF-8, and replace the key information of the database with your database information)
# Database url (specify specific database) spring.datasource.url=jdbc:mysql://::1:3306/maven_ssm?serverTimezone=UTC&characterEncoding=utf-8&useSSL=true # Database login user name spring.datasource.username=root # Database login password spring.datasource.password=root123 # Database driver class name spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
7. Add some package s under project src/main/java for simple classification management script
8. Add User script under entity package
(Note: the User parameter corresponds to the database table)
9. Add the UserMapper interface file under dao package to map to the query language of the database
(the test here only adds query database information)
package com.example.maven_srpingboot_ssm_comment_test.dao; import java.util.List; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Select; import com.example.maven_srpingboot_ssm_comment_test.entity.User; @Mapper public interface UserMapper { @Select("select * from user") List<User> searchAllUsers(); }
10. Add a test script TestController in the controller package to test the configuration file method and call whether the database configuration is successful. Here, simply query the database information
package com.example.maven_srpingboot_ssm_comment_test.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.example.maven_srpingboot_ssm_comment_test.dao.UserMapper; import com.example.maven_srpingboot_ssm_comment_test.entity.User; @RestController public class TestController { @Autowired private UserMapper mUserMapper; @RequestMapping("/test") public String testSearchAllUsers() { List<User> users = mUserMapper.searchAllUsers(); for(User user : users) { System.out.println(user.toString()); } return "testSearchAllUsers"; } }
11. As shown in the figure, select startup script, right-click Run As and run maven project
12. Enter localhost:8080/test on the web page, and the Console will print out the database information
13. Here we are SpringBoot integrates the SSM environment and is basically completed by using the annotation method