这样这三个xml文件就关联起来了。
4.SpringMVC层 4.1 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 " version="4.0"> <!--DispatcherServlet--> <servlet> <servlet-name>DispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <!--一定要注意:我们这里加载的是总的配置文件--> <param-value>classpath:applicationContext.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>DispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!--编码配置--> <filter> <filter-name>encodingFilter</filter-name> <filter-class> org.springframework.web.filter.CharacterEncodingFilter </filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!--Session过期时间--> <session-config> <session-timeout>15</session-timeout> </session-config> </web-app> 4.2 springmvc.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:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 1.开启SpringMVC注解驱动 --> <mvc:annotation-driven /> <!-- 2.静态资源默认servlet配置--> <mvc:default-servlet-handler/> <!-- 3.配置jsp 显示ViewResolver视图解析器 这部分加不加自行决定 <bean> <property value="org.springframework.web.servlet.view.JstlView" /> <property value="/WEB-INF/jsp/" /> <property value=".jsp" /> </bean> --> <!-- 4.扫描web相关的bean --> <context:component-scan base-package="com.Controller" /> </beans> 4.3 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" xsi:schemaLocation="http://www.springframework.org/schema/beans "> <import resource="spring-dao.xml"/> <import resource="spring-service.xml"/> <import resource="spring-mvc.xml"/> </beans>以上SSM架构,搭建完成
5.POJO层因为设计的数据库中有4个表,分别是:user、type、news、film
所以对应创建四个实体类
public class user { private Integer id; private String username; private String paw; private Integer tele; private String email; // 有参\有参方法 // Get\Set方法 // toString() } public class types { private Integer id; private String type; // 有参\有参方法 // Get\Set方法 // toString() }news实体类中使用了JSR303检验机制,不加注解也是可以的
public class news { @NotNull private Integer ISDN; @NotNull private String title; @NotNull private String author; @DateTimeFormat(pattern = "yyyy-MM-dd") @Past private Date date; @NotNull private String description; // 有参\有参方法 // Get\Set方法 // toString() } public class film { private Integer ISDN; private String name; private String director; private String actor; private String type; private String country; private String language; private Double score; private String photo; private String href; private String description; // 有参\有参方法 // Get\Set方法 // toString() }在这些实体类中,我使用的是直接添加构造方法;如果觉得麻烦,可以使用Lombok插件
6.Dao层每一个Dao类都分别对应着一个实体类的操作
Mapper接口+Mapper.xml
6.1 UserMapper public interface UserMapper { /** * 获取用户列表 * @return */ public List<user> getUserList(); /** * id查用户 * @return */ public user getUserById(int id); /** * 添加用户 * @param user * @return */ public int insertUser(user user); /** * 修改用户个人信息 * @return */ public int upUser(user user); /** * 修改密码 * @param user * @return */ public int uppaw(user user); } <?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"> <mapper namespace="com.zc.Dao.UserMapper"> <select resultType="com.zc.pojo.user"> select * from film.user </select> <select resultType="com.zc.pojo.user"> select * from film.user where id=#{id} </select> <insert parameterType="com.zc.pojo.user" > insert into film.user (username,paw,tele,email) values (#{username},#{paw},#{tele},#{email}) </insert> <update parameterType="com.zc.pojo.user"> update film.user set username = #{username},tele = #{tele},email = #{email} where id = #{id} </update> <update parameterType="com.zc.pojo.user"> update film.user set paw=#{paw} where id = #{id} </update> </mapper> 6.2 TypesMapper public interface TypesMapper { /** * 通过ID查找type * @param id * @return */ public types selectTypeByID(int id); /** * 查询全部 * @return */ public List<types> Alltypes(); /** * 通过id删除type * @param id * @return */ public int DeletetypeById(int id); /** * 更新type * @param types * @return */ public int Updatetype(types types); /** * 添加type * @param types * @return */ public int addtype(types types); } 6.3 NewsMapper public interface NewsMapper { /** * 通过 ISDN 查找 new * @param ISDN * @return */ public news selectNewById(int ISDN); /** * 查询全部新闻 * @return */ public List<news> Allnews(); /** * 添加新闻 * @param news * @return */ public int addNew(news news); /** * 更新新闻 * @param news * @return */ public int upNew(news news); /** * 通过 ISDN 删除新闻 * @param ISDN * @return */ public int DelnewById(int ISDN); } 6.4 FilmMapper public interface FilmMapper { /** * 查找全部电影 * @return */ public List<film> AllFilm(); /** * 通过ISDN查电影 * @param ISDN * @return */ public List<film> selectfilmByISDN(int ISDN); /** * 通过导演查电影 * @param director * @return */ public List<film> selectfilmByDir(String director); /** * 通过类型查电影 * @param type * @return */ public List<film> selectfilmBytype(String type); /** * 添加电影 * @param film * @return */ public int AddFilm(film film); /** * 修改电影 * @param film * @return */ public int upFilm(film film); /** * 通过ISDN 删除电影 * @param ISDN * @return */ public int DelFilm(int ISDN); } 7.Service层