Open idea File->New->Project to create a new maven web project
Next fill in GroupId and ArtifactId
The next step is to select the local maven installation path. If you have not installed it, you can click to enter: Maven configuration under IntelliJ IDEA
Next project name and storage path
finish, the directory structure of the complete project is shown in the following figure:
Then start configuring pom.xml
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> <!--Unified definition of version information--> <springframework.version>4.2.2.RELEASE</springframework.version> <servlet-api.version>3.1.0</servlet-api.version> <jstl.version>1.1.2</jstl.version> <mybatis.version>3.4.1</mybatis.version> <jdbc.version>5.1.35</jdbc.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>${servlet-api.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${springframework.version}</version> </dependency> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>${jstl.version}</version> </dependency> <!-- Database connection pool --> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.2</version> </dependency> <!-- database driver--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${jdbc.version}</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> </dependencies>
web.xml
<!--Mainly monitor Spring configuration file changes--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mybatis.xml</param-value> </context-param> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- Configure the location and name of the configuration file under SpringMVC--> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!--Configure encoding filter, set encoding: utf-8 set mandatory encoding--> <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> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <!--Configure encoding filter, set all resources--> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list>
spring-mvc.xml
<!-- Automatically scan all classes under the controller package--> <context:component-scan base-package="com.xiaodou.ssm.controller"/> <mvc:annotation-driven></mvc:annotation-driven> <!-- configure view resolver --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name = "prefix" value="/WEB-INF/jsp/"></property> <property name = "suffix" value = ".jsp"></property> </bean>
jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/xiaodou?useUnicode=true&characterEncoding=utf8 jdbc.username=root jdbc.password=123456 c3p0.maxPoolSize=30 c3p0.minPoolSize=10 c3p0.autoCommitOnClose=false c3p0.checkoutTimeout=10000 c3p0.acquireRetryAttempts=2
spring-mybatis.xml
<!-- Scan all types of annotations under the service package--> <context:component-scan base-package="com.xiaodou.ssm.service.impl"/> <!-- 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.xiaodou.ssm.domain"/> <!-- Scan sql configuration file: xml file required by mapper--> <property name="mapperLocations" value="classpath:mapper/*.xml"/> </bean> <!-- Configure the scanning 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.xiaodou.ssm.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"/>
Once configured, add some examples to test it out.
Add a UserMapper.xml to mapper (Dao and Service are omitted here)
<mapper namespace="com.xiaodou.ssm.dao.UserMapper"> <select id="getUserInfo" parameterType="java.lang.String" resultType="com.xiaodou.ssm.domain.User"> select * from user where id=#{id} </select> </mapper>
Create a new MainController
@Controller public class MainController { @RequestMapping("/index") public ModelAndView index(){ ModelAndView modelAndView = new ModelAndView(); modelAndView.addObject("id","3"); modelAndView.setViewName("index"); return modelAndView; } }
UserController
@Controller @RequestMapping("/user") public class UserController { @Autowired private UserService userService; @RequestMapping("/getUserInfo") public ModelAndView getUserInfo(String id){ ModelAndView modelAndView = new ModelAndView(); User user = userService.getUserInfo(id); modelAndView.addObject("user",user); modelAndView.setViewName("user"); return modelAndView; } }
operation result
At the end of this, a basic ssm project is built.