Spring日记_02之搭建一个新项目

2023-05-25,,

<!--
@page { margin: 2cm }
h5 { margin-top: 0.49cm; margin-bottom: 0.51cm; direction: ltr; line-height: 156%; text-align: justify; page-break-inside: avoid; orphans: 0; widows: 0 }
h5.western { font-family: "Calibri", serif; font-size: 14pt }
h5.cjk { font-family: "宋体"; font-size: 14pt }
h5.ctl { font-family: ; font-size: 14pt }
h4 { margin-top: 0.49cm; margin-bottom: 0.51cm; direction: ltr; line-height: 156%; text-align: justify; page-break-inside: avoid; orphans: 0; widows: 0 }
h4.western { font-family: "Calibri Light", serif; font-size: 14pt }
h4.cjk { font-family: "宋体"; font-size: 14pt }
h4.ctl { font-family: ; font-size: 14pt }
h2 { margin-top: 0.46cm; margin-bottom: 0.46cm; direction: ltr; line-height: 173%; text-align: justify; page-break-inside: avoid; orphans: 0; widows: 0 }
h2.western { font-family: "Calibri Light", serif; font-size: 16pt }
h2.cjk { font-family: "宋体"; font-size: 16pt }
h2.ctl { font-family: ; font-size: 16pt }
p { margin-bottom: 0.25cm; direction: ltr; line-height: 115%; text-align: justify; orphans: 0; widows: 0 }
a:link { color: #0563c1 }
-->

程序

表现层 业务层 持久层

从持久层开始写

总结如何搭建一个项目

1、新建一个Maven项目

2、可能新建之后会有错,右键Deployment
Descriptor: note, 选择Generate
Deployment Descriptor Stub

3、使用maven仓库导包

<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc-portlet</artifactId>
<version>3.2.8.RELEASE</version>
</dependency> <dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.5</version>
</dependency> <dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.5</version>
</dependency> <dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.8.5</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>3.2.8.RELEASE</version>
</dependency> <dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.0</version>
</dependency> <dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency> <dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency> <dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency> </dependencies>

dependencies

3.1、查看一下下载的包

如果maven的包迟迟不下载,就右键项目,选择maven – UpdateProject

4、 为项目指定tomcat运行环境

右键项目 – Properties – Targeted Runtimes

5.在下面server的tomcat服务器中添加项目

6 、在src/main/resources下添加两个文件夹

7 、 Spring配置文件

先将原项目的两个配置文件 spring-service.xml和 spring-mybatis.xml 复制过来

将spring-service.xml 改名为spring-web.xml,因为这样更合适一点

spring-mybatis.xml注意改一下文件里的包名,其他的关于mybatis的不用改

spring-web.xml如下:
<?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"> <!-- 启动注解版本的Spring MVC -->
<mvc:annotation-driven></mvc:annotation-driven>
<context:component-scan base-package="cn.edu.sdu.wh.controller"> </context:component-scan> </beans>

spring-web.xml

spring-mybatis.xml如下:
<?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"> <!-- 配置dbcp连接池:连接到数据库 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<!-- 连接池的基本连接参数 -->
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/cloud_note"></property>
<property name="username" value="root"></property>
<property name="password" value="123"></property>
<!-- 连接池可选参数 -->
<property name="maxActive" value="50"></property>
<property name="initialSize" value="5"></property>
<property name="maxIdle" value="5"></property>
</bean> <!-- 配置MyBatis的 Session 工厂 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- 声明MyBatis SQL 声明文件保存的地方 -->
<property name="mapperLocations" value="classpath:mapper/*.xml"></property>
</bean> <!-- 配置MyBatis的自动接口扫描 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 数据访问接口的存储位置 -->
<property name="basePackage" value="cn.edu.sdu.wh.dao"></property>
</bean>
</beans>

spring-mybatis.xml

8 、 相应的在src/main/java下添加两个包

9 、 新建一个Servlet

可以查看src/main/webapp/WEB-INF下的web.xml,验证一下

10 、 将Tomcat 发布 然后 运行!

Spring日记_02之搭建一个新项目的相关教程结束。

《Spring日记_02之搭建一个新项目.doc》

下载本文的Word格式文档,以方便收藏与打印。