这是我写的第二个SSM完整的小项目,是一个优秀电影推荐推荐系统,分享出来供大家交流分享。
项目结构目录
项目主要运行截图
项目的主页面
信息展示页面(所有电影显示列表)
增加优秀电影页面
查看优秀电影页面
修改优秀电影页面
删除优秀电影
项目的主要配置文件(类似于上一个SSM项目)
spring-dao数据库持久层配置文件
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!--1、关联数据库文件-->
<context:property-placeholder location="classpath:database.properties"/>
<!--2、连接池-->
<!--
C3p0:自动化操作(自动化的加载配置文件,并且可以自动设置到对象中)
jdbc:半自动化操作,不能自动连接
druid:
hikari:
-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${driver}"/>
<property name="jdbcUrl" value="${url}"/>
<property name="user" value="${user}"/>
<property name="password" value="${password}"/>
<!--c3p0的私有属性-->
<property name="maxPoolSize" value="30"/>
<property name="minPoolSize" value="10"/>
<!--关闭连接后不能自动连接commit-->
<property name="autoCommitOnClose" value="false"/>
<!--获取连接超时时间,10秒-->
<property name="checkoutTimeout" value="10000"/>
<!--当前获取连接失败的次数-->
<property name="acquireRetryAttempts" value="2"/>
</bean>
<!-- 3.配置SqlSessionFactory对象 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入数据库连接池 -->
<property name="dataSource" ref="dataSource"/>
<!-- 配置MyBaties全局配置文件:mybatis-config.xml -->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<!-- 扫描pojo包 使用别名 -->
<property name="typeAliasesPackage" value="com.mengxiaoyu.pojo"/>
<!-- 扫描sql配置文件:mapper需要的xml文件 -->
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
</bean>
<!-- 4.配置扫描Dao接口包,动态实现Dao接口,注入到spring容器中 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 注入sqlSessionFactory -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!-- 给出需要扫描Dao接口包 -->
<property name="basePackage" value="com.mengxiaoyu.dao"/>
</bean>
</beans>
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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!--1、注解驱动-->
<mvc:annotation-driven/>
<!--2、静态资源过滤器-->
<mvc:default-servlet-handler/>
<!-- 3、扫描包:controller -->
<context:component-scan base-package="com.mengxiaoyu.controller"/>
<!-- 4、视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--前缀-->
<property name="prefix" value="/WEB-INF/pages/"/>
<!--后缀-->
<property name="suffix" value=".jsp"/>
</bean>
</beans>
spring-service业务层配置
<?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:tx="http://www.springframework.org/schema/tx"
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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 扫描service包下所有使用注解的类型 -->
<context:component-scan base-package="com.mengxiaoyu.service" />
<!-- 配置事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 注入数据库连接池 -->
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 配置基于注解的声明式事务 -->
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
项目核心类
FilmController控制跳转类
package com.mengxiaoyu.controller;
import com.mengxiaoyu.pojo.Film;
import com.mengxiaoyu.service.FilmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
import java.util.Map;
@RequestMapping("filmController")
@Controller
public class FilmController {
@Autowired
private FilmService filmService;
@RequestMapping("getAllFilm")
public String getAllFilm(Map<String, List<Film>> map) {
List<Film> films = filmService.queryAllFilm();
map.put("films", films);
return "homePage";
}
@ResponseBody
@RequestMapping("addFilm")
public String addFilm(Film film) {
//加入数据库,看返回值决定是否成功
int i = filmService.addFilm(film);
if (i > 0) {
return "success";
} else {
return "error";
}
}
@ResponseBody
@RequestMapping("deleteFilmById")
public String deleteFilmById(int id) {
int i = filmService.deleteFilmById(id);
if (i > 0) {
return "success";
} else {
return "error";
}
}
@ResponseBody
@RequestMapping("updateFilm")
public String updateFilm(Film film) {
//加入数据库,看返回值决定是否成功
int i = filmService.updateFilm(film);
if (i > 0) {
return "success";
} else {
return "error";
}
}
}
FilmDao类的映射文件FilmMapper.xml
<?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">
<!-- namespace:该mapper.xml映射文件的唯一标识 -->
<mapper namespace="com.mengxiaoyu.dao.FilmDao">
<select id="queryFilmById" parameterType="int" resultType="Film">
select * from film where id = #{id}
</select>
<select id="queryAllFilm" resultType="Film">
select * from film
</select>
<insert id="addFilm" parameterType="Film">
insert into film(name,director,actor,date,nation,grade) values (#{name}, #{director}, #{actor}, #{date}, #{nation}, #{grade})
</insert>
<delete id="deleteFilmById" parameterType="int">
delete from film where id = #{id}
</delete>
<update id="updateFilm" parameterType="Film">
update film set name = #{name}, director = #{director}, actor = #{actor}, date = #{date}, nation = #{nation}, grade = #{grade} where id = #{id}
</update>
</mapper>
更多源代码请去我的github下载源项目