环境搭建
导入相关jar包:
1、数据库驱动包
mysql-connector-java-8.0.17.jar
2、DBCP连接池包
commons-dbcp2-2.7.0.jar
commons-pool2-2.7.0.jar
commons-logging-1.2.jar
3、日志包
log4j-1.2.17.jar
4、mybatis相关包
mybatis-3.5.2.jar
5、mybatis和spring整合包
mybatis-spring-2.0.1.jar
6、spring所有包
spring-aop-5.1.9.RELEASE.jar
spring-aspects-5.1.9.RELEASE.jar
spring-beans-5.1.9.RELEASE.jar
spring-context-5.1.9.RELEASE.jar
spring-context-indexer-5.1.9.RELEASE.jar
spring-context-support-5.1.9.RELEASE.jar
spring-core-5.1.9.RELEASE.jar
spring-expression-5.1.9.RELEASE.jar
spring-instrument-5.1.9.RELEASE.jar
spring-jcl-5.1.9.RELEASE.jar
spring-jdbc-5.1.9.RELEASE.jar
spring-jms-5.1.9.RELEASE.jar
spring-messaging-5.1.9.RELEASE.jar
spring-orm-5.1.9.RELEASE.jar
spring-oxm-5.1.9.RELEASE.jar
spring-test-5.1.9.RELEASE.jar
spring-tx-5.1.9.RELEASE.jar
spring-web-5.1.9.RELEASE.jar
spring-webflux-5.1.9.RELEASE.jar
spring-webmvc-5.1.9.RELEASE.jar
spring-websocket-5.1.9.RELEASE.jar
7、jstl包
jstl-1.2.jar
8、spring依赖的aspect包
aspectjweaver-1.9.4.jar
准备配置文件
1、在项目下新建资源文件夹config
新建db.properties填写数据库相关配置,每句后面不能留空格
1 2 3 4
| jdbc.driver=com.mysql.cj.jdbc.Driver jdbc.url=jdbc:mysql:///test?serverTimezone=UTC jdbc.username=root jdbc.password=root
|
新建log4j.properties添加日志输出配置,每句后面不能留空格
1 2 3 4 5 6 7 8
| log4j.rootLogger = debug,Console
log4j.appender.Console = org.apache.log4j.ConsoleAppender log4j.appender.Console.Target = System.out log4j.appender.Console.layout = org.apache.log4j.PatternLayout log4j.appender.Console.layout.ConversionPattern = %d{ABSOLUTE} %5p %c{1}:%L - %m%n
|
2、在config下新建mybatis包存放mybatis配置文件
新建sqlMapConfig.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| <?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><?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> <typeAliases> <package name="com.syl.entity" /> </typeAliases>
</configuration>
|
3、在config下新建spring包存放spring配置文件
新建applicationContext-dao.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| <?xml version="1.0" encoding="UTF-8" ?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd 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.xsd ">
<context:property-placeholder location="classpath:db.properties" /> <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}"></property> <property name="username" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> <property name="maxTotal" value="30"></property> <property name="maxIdle" value="5"></property> </bean>
<bean id="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml" /> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.syl.mapper" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean>
</beans>
|
注意事项:
1 2
| <property name="sqlSessionFactoryBeanName" ref="sqlSessionFactory"></property>
|
原因:class=”org.mybatis.spring.mapper.MapperScannerConfigurer”,而这句代码会在数据源加载前就执行了,之后就把表达式${jdbc.driver}当成字符串执行了。
4、新建applicationContext-service.xml,管理service层的类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| <?xml version="1.0" encoding="UTF-8" ?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd 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.xsd ">
<bean id="studentServiceImpl" class="com.syl.service.Impl.StudentServiceImpl"></bean> </beans>
|
5、新建applicationContext-transaction.xml,配置事务管理
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| <?xml version="1.0" encoding="UTF-8" ?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd 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.xsd ">
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="save" propagation="REQUIRED" /> <tx:method name="delete" propagation="REQUIRED" /> <tx:method name="insert" propagation="REQUIRED" /> <tx:method name="update" propagation="REQUIRED" /> <tx:method name="find" propagation="SUPPORTS" read-only="true" /> <tx:method name="select*" propagation="SUPPORTS" read-only="true" /> <tx:method name="get*" propagation="SUPPORTS" read-only="true" /> </tx:attributes> </tx:advice>
<aop:config> <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.syl.service.Impl.*.*(..))" /> </aop:config> </beans>
|
6、新建springmvc.xml配置springmvc相关
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| <?xml version="1.0" encoding="UTF-8" ?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd 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.xsd ">
<context:component-scan base-package="com.syl.controller" /> <mvc:annotation-driven /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> </bean>
</beans>
|
7、配置web.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0"> <display-name>ssm</display-name>
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/applicationContext-*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
<servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/springmvc.xml</param-value> </init-param> </servlet>
<servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping>
<welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app>
|
创建所有相关包
控制层 com.syl.controller
service层 com.syl.service.Impl
mapper com.syl.mapper
entity com.syl.entity
准备数据库
数据库:test
数据表:student、teacher、stu_tea
准备mybatis逆向工程,生成相关的mapper以及实体类
1、新建java项目,引入jar包
asm-7.0.jar
cglib-3.2.10.jar
log4j-1.2.17.jar
mybatis-3.5.2.jar
mybatis-generator-core-1.3.7.jar
mysql-connector-java-8.0.17.jar
2、generatorConfig.xml配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration> <context id="testTables" targetRuntime="MyBatis3"> <commentGenerator> <property name="suppressAllComments" value="true" /> </commentGenerator> <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" onnectionURL="jdbc:mysql://localhost:3306/test?serverTimezone=UTC" userId="root" password="root"> <property name ="nullCatalogMeansCurrent" value ="true"/> </jdbcConnection>
<javaTypeResolver> <property name="forceBigDecimals" value="false" /> </javaTypeResolver>
<javaModelGenerator targetPackage="com.syl.entity" targetProject=".\src"> <property name="enableSubPackages" value="false" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <sqlMapGenerator targetPackage="com.syl.mapper" targetProject=".\src"> <property name="enableSubPackages" value="false" /> </sqlMapGenerator> <javaClientGenerator type="XMLMAPPER" targetPackage="com.syl.mapper" targetProject=".\src"> <property name="enableSubPackages" value="false" /> </javaClientGenerator> <table tableName="student"></table> <table tableName="teacher"></table> <table tableName="stu_tea"></table> </context> </generatorConfiguration>
|
3、java文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| import java.io.File; import java.util.ArrayList; import java.util.List;
import org.mybatis.generator.api.MyBatisGenerator; import org.mybatis.generator.config.Configuration; import org.mybatis.generator.config.xml.ConfigurationParser; import org.mybatis.generator.internal.DefaultShellCallback;
public class GeneratorTest { public static void main(String[] args) throws Exception {
List<String> warnings = new ArrayList<String>(); boolean overwrite = true; File configFile = new File("generatorConfig.xml"); ConfigurationParser cp = new ConfigurationParser(warnings); Configuration config = cp.parseConfiguration(configFile); DefaultShellCallback callback = new DefaultShellCallback(overwrite); MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings); myBatisGenerator.generate(null); }
}
|
除了配置文件的一些参数不固定,其他的都是固定的不用修改
将生成的mapper以及实体类添加到项目中
项目搭建完成
项目添加功能
由于实际项目开发涉及的数据表都不止一张,所以需要自定义学生mapper接口以及配置文件、相关实体类
StudentCustom.java实体类,可以先放着什么都不写,后续用到再添加
1 2
| public class StudentCostom extends Student{ }
|
StudentsQueryVo.java实体类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| public class StudentsQueryVo { private Student student; private StudentCustom studentCustom; public Student getStudent() { return student; } public void setStudent(Student student) { this.student = student; } public StudentCustom getStudentCustom() { return studentCustom; } public void setStudentCustom(StudentCustom studentCustom) { this.studentCustom = studentCustom; } }
|
StudentMapperCustom.java接口
1 2 3 4
| public interface StudentMapperCustom { List<StudentCostom> findStudnetByOthers(StudentsQueryVo studentsQueryVo) throws Exception; }
|
studentMapperCustom.xml配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| <?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.syl.mapper.StudentMapperCustom"> <sql id="query_student_by_others"> <if test="studentCustom!=null"> <if test="studentCustom.name!=null and studentCustom.name!=''"> <bind name="studentCustom.name" value="'%' + studentCustom.name + '%'"/> sname like '#{studentCustom.name}' </if> </if> </sql>
<select id="findStudnetByOthers" parameterType="StudentsQueryVo" resultType="com.syl.entity.StudentCustom"> select * from student <where> <include refid="query_student_by_others"></include> </where> </select> </mapper>
|
service层接口:
1 2 3
| public interface StudentService { public List<StudentCostom> findStudentsList(StudentsQueryVo studentsQueryVo) throws Exception; }
|
service实现类
1 2 3 4 5 6 7 8 9 10 11
| @Service public class StudentServiceImpl implements StudentService {
@Autowired private StudentMapperCustom smc; @Override public List<StudentCostom> findStudentsList(StudentsQueryVo studentsQueryVo) throws Exception { return smc.findStudnetByOthers(studentsQueryVo); }
}
|
控制层StudentController.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| @Controller public class StudentController {
@Autowired private StudentService studentService;
@RequestMapping("findStudentsList") public ModelAndView findStudentsList() throws Exception { List<StudentCostom> studentsList = studentService.findStudentsList(null); ModelAndView modelAndView = new ModelAndView(); modelAndView.addObject("studentsList", studentsList); modelAndView.setViewName("index.jsp"); return modelAndView; } }
|
至此,ssm整合完成,后续功能可以随心添加。