1.创建工程
(1)新建空项目 empty project
(2)创建父工程以及子模块(都是new moudle–maven)
parent打包方式pom
entity存放实体类,打包方式jar
webui存放前端页面,打包方式war
component存放后端组件,service等,打包方式jar
util放工具类,打包方式jar
reverse打包方式jar
打包方式:
jar
2.创建子工程之间的依赖
webui 依赖 component
component 依赖 entity
component 依赖 uti
2.创建数据库和数据表
3.MyBatis 逆向工程环境搭建
reverse文件就是配置mybatis逆向工程的
(1)pom.xml添加依赖和插件
添加依赖:
<!--添加依赖mybatis核心包-->
添加插件:
<!-- 具体插件mybatis-generator-maven-plugin,逆向工程的操作是以构建过程中插件形式出现的 -->
添加插件依赖:
<!-- 逆向工程的核心依赖mybatis-generator-core-->
<!--数据库连接池-->
<!--mysql驱动依赖-->
(2)resources文件下新建generatorConfig.xml
<!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
<!--加入存放实体类的包-->
(3)执行逆向生成操作的 Maven 命令
RUN–edit configuration
<!--填入下面这句话-->
mybatis-generator:generate
然后运行一下检验是否导入成功
5.父工程依赖管理
(1)版本声明(修改pom.xml)
(2)依赖管理(修改pom.xml)
<!-- Spring 依赖 -->
<artifactId>spring-orm</artifactId>
<artifactId>spring-webmvc</artifactId>
<artifactId>spring-test</artifactId>
<!--AOP依赖-->
<artifactId>aspectjweaver</artifactId>
<artifactId>cglib</artifactId>
<!-- MySQL 驱动 -->
<artifactId>mysql-connector-java</artifactId>
<!-- 数据源 -->
<artifactId>druid</artifactId>
<!-- MyBatis依赖-->
<artifactId>mybatis</artifactId>
<!-- MyBatis 与 Spring 整合依赖-->
<artifactId>mybatis-spring</artifactId>
<!-- MyBatis 分页插件 -->
<artifactId>pagehelper</artifactId>
<!-- Spring 进行 JSON 数据转换依赖 -->
<artifactId>jackson-core</artifactId>
<artifactId>jackson-databind</artifactId>
<!-- JSTL 标签库 -->
<artifactId>jstl</artifactId>
<!-- junit 测试 -->
<artifactId>junit</artifactId>
<!-- 引入 Servlet 容器中相关依赖 -->
<artifactId>servlet-api</artifactId>
<!-- JSP 页面使用的依赖 -->
<artifactId>jsp-api</artifactId>
依赖信息来源
https://mvnrepository.com/
6.spring整合mybatis
(1)整合思路
MyBatis动态生成的代理类不能用context:component-scan来进行扫描,所以使用MapperScanConfig
(2)子工程中加入依赖
(课程选择component,本人选择webui)
<!--spring依赖-->
<artifactId>spring-orm</artifactId>
<artifactId>spring-webmvc</artifactId>
<!--AOP依赖-->
<artifactId>aspectjweaver</artifactId>
<artifactId>cglib</artifactId>
<!-- MySQL 驱动 -->
<artifactId>mysql-connector-java</artifactId>
<!-- 数据源 -->
<artifactId>druid</artifactId>
<!-- MyBatis -->
<artifactId>mybatis</artifactId>
<!-- MyBatis 与 Spring 整合 -->
<artifactId>mybatis-spring</artifactId>
<!-- MyBatis 分页插件 -->
<artifactId>pagehelper</artifactId>
<!-- Spring 进行 JSON 数据转换依赖 -->
<artifactId>jackson-core</artifactId>
<artifactId>jackson-databind</artifactId>
<!-- JSTL 标签库 -->
<artifactId>jstl</artifactId>
(2)在webui下配置xml文件
1.创建mybatis全局配置文件mybatis-config.xml
<!--名誉主席-->
2.创建spring配置文件(spring-persist-mybatis.xml)
(看过本人·其他文章的直接新建appliacationContext.xml)
<!-- 配置数据源 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<!--数据库信息-->
<property name="username" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
<property name="url" value="${jdbc.url}"/>
<property name="driverClassName" value="${jdbc.driver}"/>
</bean>
<!--配置SqlSessionFactoryBean-->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 装配数据源 -->
<property name="dataSource" ref="dataSource"/>
<!-- 指定 MyBatis 全局配置文件位置 -->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<!-- 指定 Mapper 配置文件位置 -->
<property name="mapperLocations" value="classpath:mybatis/mapper/*Mapper.xml"/>
</bean>
<!-- 配置 MapperScannerConfigurer -->
<!-- 把 MyBatis 创建的 Mapper 接口类型的代理对象扫描到 IOC 容器中 -->
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 使用 basePackage 属性指定 Mapper 接口所在包 -->
<property name="basePackage" value="com.atguigu.crowd.mapper"/>
</bean>
6.声明式事务
(1)思路
增加一个新的配置文件spring-persist-tx.xml
(2)加入AOP所需依赖
<!-- AOP 所需依赖 -->
<artifactId>aspectjweaver</artifactId>
<artifactId>cglib</artifactId>
(3)配置spring-persist-tx.xml
<!-- 配置事务管理器 -->
<bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 装配数据源 -->
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 配置 AOP -->
<aop:config>
<!-- 配置切入点表达式 -->
<aop:pointcut expression="execution(* *..*Service.*(..))" id="txPointCut"/>
<!-- 将事务通知和切入点表达式关联到一起 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
</aop:config>
<!-- 配置事务通知 -->
<tx:advice id="txAdvice" transaction-manager="dataSourceTransactionManager">
<tx:attributes>
<!-- name 属性指定当前要配置的事务方法的方法名 -->
<tx:method name="get*" read-only="true"/>
<tx:method name="query*" read-only="true"/>
<tx:method name="find*" read-only="true"/>
<tx:method name="count*" read-only="true"/>
<!-- propagation 属性配置事务方法的传播行为,建议值:REQUIRES_NEW-->
<!-- rollback-for 属性配置回滚的异常,建议值:java.lang.Exception-->
<tx:method name="save*" propagation="REQUIRES_NEW"
rollback-for="java.lang.Exception" />
<tx:method name="remove*" propagation="REQUIRES_NEW"
rollback-for="java.lang.Exception" />
<tx:method name="update*" propagation="REQUIRES_NEW"
rollback-for="java.lang.Exception" />
</tx:attributes>
</tx:advice>
7.表述层工作机制
(1)思路(2)加入依赖
<artifactId>spring-webmvc</artifactId>
(3)配置web.xml
<!--ContextLoaderListener
作用:加载 Spring 的配置文件,根据 Spring 的配置文件初始化 IOC容器-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-persist-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--配置中央调度器-->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-web-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>*.html</url-pattern>
<url-pattern>*.json</url-pattern>
</servlet-mapping>
(4)创建并配置springmvc的配置文件spring-web-mvc.xml
创建要扫描的包,本人选择webui,课程选择component
<context:component-scan base-package="com.atguigu.crowd.mvc"/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/"/>
<property name="suffix" value=".jsp"/>
</bean>
<mvc:annotation-driven/>
8.Ajax请求
(1)component的pom.xml引入依赖
<!-- servlet使用的依赖 -->
<artifactId>servlet-api</artifactId>
<!-- JSP 页面使用的依赖 -->
<artifactId>jsp-api</artifactId>
(2)加入jQuery
(3)加入ajax在index.jsp下
<head>
<script type="text/javascript">
$(function () {
$("#btn1").click(function () {
$.ajax({
"url": "send/array.html", /*请求目标资源地址*/
"type": "post",
"data":{"array":[5,6,7]}, /*要发送的数据*/,
"contentType": "application/json;charset=UTF-8",
});
});
});
</script>
</head>
<body>
<button id="btn1">点击</button>
</body>
以上两步在webui下进行
(4)component下 接收数据
写下controller层
@Controller
public class MyController {
@RequestBody
@RequestMapping("/send/array.html")
public String test01(@RequestBody List<Integer> array){
//@RequestBody用来接收前端传递给后端的json字符串中的数据的
}
}
/*
参数注解
RequestParam:发送简单数据
RequestBody;要发送复杂对象,如json
*/
9.跳转到登录页面
<mvc:view-controller path="请求地址" view-name="admin-login"/>