个人博客项目的练习记录(一)

个人博客项目的练习记录(一)

一个根据视频练习的小笔记,会做得详细一点,也会记录一下自己的疑问。如果觉得我写的有问题,欢迎指正。

框架:ssm
工具:eclipse
数据库:mysql

一、创建Maven项目

在这里插入图片描述
新建maven项目

新建完之后会报错,这是因为在src下面没有web.xml文件;
选中DD:blog(如下图)-右键-选中Generate Deployment Descriptor Stub,标红消失。
建好之后的目录
再点击项目-右键-properties-Targeted Runtimes,如下图:
tomcat

二、导入各种所需的jar包

可以直接在pom.xml中add添加,也可以在maven官网复制粘贴代码到文件里面。

需要导入的jar包的作用:

  • spring-webmvc:springmvc所需包
  • spring-jdbc:jdbc所需包
  • mybatis
  • commons-dbcp:连接池所需包
  • mysql-connector-java:数据库驱动
  • mybatis-spring:mybatis整合spring所需包
  • jackson-databind:jackson所需包
  • jstl:用jsp需要这个

框架整合只需要mybatis-spring的整合包,却不需要springmvc和spring的整合包,好像是因为springmvc本来就是spring的一部分。【2020.3.24】
添加jar包

三、构建基本目录

1、包目录

根据mvc分层思想,创建包目录

  • controller:控制层,用于接收请求,并返回数据或者转发页面
  • dao:持久层,用来写sql
  • service:业务逻辑层,用来处理事务;代码逻辑的实现就写在service层
  • view:实体类

发送请求到控制层,再调用service层的方法去处理事务,事务要用到数据库的时候去dao层调用sql。【2020.3.24】
包目录

2、后台页面目录

创建一个目录名字叫page,用来存放页面文件。因为WEB-INF目录是不可见的,所以其下文件是安全的。一般来说存放的是需要登录后才可以访问的后台管理页面。
后台页面目录

3、静态页面目录

在src\main\webapp下创建static,该目录下存放前端样式文件
静态页面目录

4、默认首页

在src\main\webapp下创建index.jsp文件
目录

四、文件配置

1、数据库配置

建立数据库db_myblog,字符集是utf8,字符集校对规则是utf8_general_ci
新建一个数据库配置文件db_properties
配置文件目录
db_properties内容:

url=jdbc:mysql://localhost:3306/db_myblog
driverClass=com.mysql.jdbc.Driver
user=root
password=123
2、spring-mybatis整合配置文件

加载配置文件
配置数据库连接参数
配置SqlSessionFactoryBean
配置文件代码如下:

<?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:jdbc="http://www.springframework.org/schema/jdbc"
       xmlns:jee="http://www.springframework.org/schema/jee"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:cache="http://www.springframework.org/schema/cache"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
        http://www.springframework.org/schema/cache
        http://www.springframework.org/schema/cache/spring-cache-3.2.xsd">

    <!-- 加载配置文件 -->
    <util:properties id="config" location="classpath:db.properties"></util:properties>
    <!-- 配置数据库连接参数以及连接池 -->
    <bean id="ds" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="#{config.driver}" />
        <property name="url" value="#{config.url}" />
        <property name="username" value="#{config.username}" />
        <property name="password" value="#{config.password}" />
    </bean>
    
    <!--spring集成mybatis,不需要mybatis的配置文件-->
    <!-- 配置SqlSessionFactoryBean -->
    <bean id="ssfb" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 注入连接池 -->
        <property name="dataSource" ref="ds" />
        <!-- 自动扫描映射文件 -->
        <property name="mapperLocations" value="classpath:cn/free/blog/dao/*.xml"></property>
    </bean>
    
    <!--DAO接口所在包名,Spring会自动查找其下的类 ,自动扫描了所有的XxxxMapper.xml对应的mapper接口文件,只要Mapper接口类和Mapper映射文件对应起来就可以了-->
    <!-- 该bean负责调用SqlSession的getMapper函数 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="cn.free.blog.dao" />
    </bean>
    
    <!-- 开启事务注解驱动 -->
    <tx:annotation-driven/>
    
    <!-- 事务管理 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="ds" />
    </bean>

</beans>
3、spring-mvc整合配置文件
<?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:jdbc="http://www.springframework.org/schema/jdbc"  
	xmlns:jee="http://www.springframework.org/schema/jee" 
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:util="http://www.springframework.org/schema/util"
	xmlns:jpa="http://www.springframework.org/schema/data/jpa"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
	<!-- 配置组件扫描 -->
	<context:component-scan base-package="cn.free.blog" />
	<!-- 配置MVC注解扫描 -->
	<mvc:annotation-driven />
	<!-- 配置视图解析器 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/page/" /><!-- 前缀 -->
		<property name="suffix" value=".jsp" /><!-- 后缀 -->
	</bean>
	

</beans>
4、web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
  <display-name>blog</display-name>
  <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>
  
  <!-- spring mvc请求响应 -->
  <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-*.xml</param-value>
  </init-param>
  </servlet>
  
  <servlet-mapping>
  <servlet-name>SpringMVC</servlet-name>
  <url-pattern>*.action</url-pattern>
  </servlet-mapping>
  
  <servlet-mapping>
  <servlet-name>SpringMVC</servlet-name>
  <url-pattern>*.json</url-pattern>
  </servlet-mapping>
</web-app>

这个视频讲得挺乱,等我整理完再来更新笔记好了【2020.3.25】

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值