一、项目布局
使用的编辑工具 idea2020 数据库 mysql5.1 前端代码因时间仓促就没怎么写。
1.1:导包:导入之后记得还标记为库。这样才能使用jar包。
1.2:导包完成后就需要创建SQL数据库和建表。因为这个项目相对来说算是一个入门SSM的小项目。数据库中的表就只有两张表 t_empployee和t_user两张表
t_empployee
t_user
1.3建库建表完成后就需要写配置文件
首先是Spring的核心配置文件 applicationContext.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"
xsi:schemaLocation="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">
<!-- 加载jdbc.properties配置文件-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 扫描service包 -->
<context:component-scan base-package="com.lzq.service"/>
<!-- 搭建数据库数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="url" value="${jdbc.url}"/>
<property name="driverClassName" value="${jdbc.driverClassName}"/>
</bean>
<!-- MyBatis 配置SqlSessionFactoryBean对象-->
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 依赖dataSource-->
<property name="dataSource" ref="dataSource"/>
<!-- 设置公共包别名-->
<property name="typeAliasesPackage" value="com.lzq.domain"/>
<!-- 加载指定路径下的mapper.xml文件-->
<property name="mapperLocations" value="classpath:com/lzq/mapper/*.xml"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 扫描该包下的接口,只要是接口。底层就会创建实现类,去实现该包下的接口。-->
<property name="basePackage" value="com.lzq.mapper"/>
</bean>
</beans>
SpringMVC配置文件 SpringMVC.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
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
">
<!-- SpringMVC配置文件-->
<!-- 扫描包下所有的文件-->
<context:component-scan base-package="com.lzq.controller"/>
<!-- 开启扫描-->
<mvc:annotation-driven/>
<!-- 加载静态资源-->
<mvc:default-servlet-handler/>
<!-- 视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
web.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version=