数据库结构设计
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.2</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>5.3.24</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.24</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.3.24</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.24</version> </dependency> <dependency> <groupId>aopalliance</groupId> <artifactId>aopalliance</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.9.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.31</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>3.0.0</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.11</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>2.19.0</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.19.0</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> </dependency> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-dbcp2</artifactId> <version>2.9.0</version> </dependency> <dependency> <groupId>org.webjars</groupId> <artifactId>bootstrap</artifactId> <version>3.4.1</version> </dependency> <build> <finalName>PetSSM</finalName> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> <include>**/*.properties</include> </includes> <filtering>false</filtering> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.xml</include> <include>**/*.properties</include> </includes> <filtering>false</filtering> </resource> </resources> </build>
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102配置文件都要放在resources资源包目录下
jdbc.driver=com.mysql.cj.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/ssm_project jdbc.username=root jdbc.password=root 1234 2.2 创建applicationContext.xml文件
<context:property-placeholder location="classpath:database.properties"/> <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"> <property name="driverClassName" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="configLocation" value="classpath:mybatis-config.xml"/> <property name="mapperLocations"> <list> <value>classpath*:com/pet/mapper/**/*.xml</value> </list> </property> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.pet.mapper"/> </bean> <context:component-scan base-package="com.pet.service"/>
123456789101112131415161718192021222324 2.3 创建springmvc-servlet.xml配置文件<context:component-scan base-package="com.pet.controller"/> <mvc:annotation-driven/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> <!--静态资源读取--> <mvc:default-servlet-handler/> 12345678910 2.4 创建mybatis-config.xml配置文件
<configuration> <settings> <setting name="logImpl" value="LOG4J2"/> </settings> <typeAliases> <package name="com.pet.model"/> </typeAliases> </configuration> 12345678 2.5 创建log4j2.xml配置文件
<Configuration status="WARN"> <Appenders> <Console name="Console" target="SYSTEM_OUT"> <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/> </Console> </Appenders> <Loggers> <Logger name="com.pet" level="trace" additivity="false"> <AppenderRef ref="Console"/> </Logger> <Root level="error"> <AppenderRef ref="Console"/> </Root> </Loggers> </Configuration> 123456789101112131415
Pet.java
@Data @Builder @NoArgsConstructor @AllArgsConstructor public class Pet { private Integer id; private String name; private Integer age; private String type; private String description; } 1234567891011
PetMapper.java
public interface PetMapper { @Select("SELECT * FROM pets") List<Pet> getPetList(); @Insert("INSERT INTO pets(name, age, type, description) VALUES (#{name}, #{age}, #{type}, #{description})") @Options(useGeneratedKeys = true, keyProperty = "id") Integer addPet(Pet pet); @Delete("DELETE FROM pets WHERE id = #{id}") Integer deletePet(Integer id); @Update("UPDATE pets SET name=#{name}, age=#{age}, type=#{type}, description=#{description} WHERE id = #{id}") Integer updatePet(Pet pet); @Select("SELECT * FROM pets WHERE id = #{id}") Pet getPetById(Integer id); }
1234567891011121314151617185.1 PetService.java
public interface PetService { List<Pet> findPetList(); Integer add(Pet pet); Integer delete(Integer id); Integer update(Pet pet); Pet getPetById(Integer id); } 1234567891011
5.2 PetServiceImpl.java
@Service("petService") public class PetServiceImpl implements PetService { @Autowired private PetMapper petMapper; @Override public List<Pet> findPetList() { return petMapper.getPetList(); } @Override public Integer add(Pet pet) { return petMapper.addPet(pet); } @Override public Integer delete(Integer id) { return petMapper.deletePet(id); } @Override public Integer update(Pet pet) { return petMapper.updatePet(pet); } @Override public Pet getPetById(Integer id) { return petMapper.getPetById(id); } }
12345678910111213141516171819202122232425262728293031PetController.java
@Controller public class PetController { @Autowired private PetService petService; @RequestMapping("/index") public String index(Model model) { List<Pet> petList = petService.findPetList(); model.addAttribute("petList", petList); return "index"; } @RequestMapping("/add") public String addPet(@RequestParam("name") String name, @RequestParam("age") int age, @RequestParam("type") String type, @RequestParam("description") String description, Model model) { Pet pet = new Pet(); pet.setName(name); pet.setAge(age); pet.setType(type); pet.setDescription(description); petService.add(pet); return "redirect:/index"; } @RequestMapping("/del") public String deletePet(@RequestParam("id") int id) { petService.delete(id); return "redirect:/index"; } @RequestMapping("/upd") public String updatePet(@RequestParam("id") int id, Model model) { Pet pet = petService.getPetById(id); model.addAttribute("pet", pet); return "upd"; } @RequestMapping("/update") public String update(@ModelAttribute Pet pet) { petService.update(pet); return "redirect:/index"; } @RequestMapping("/goAdd") public String goAdd() { return "add"; } @RequestMapping("/goUpd") public String goUpd() { return "upd"; } }
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556<servlet> <servlet-name>PetSSM</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>PetSSM</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!--编码过滤 预防乱码--> <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> <init-param> <param-name>forceRequestEncoding</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>forceResponseEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
1234567891011121314151617181920212223242526272829303132333435363738394041 7.2 index.jsp 宠物信息列表<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>宠物信息列表</title> <link rel="stylesheet" href="${pageContext.request.contextPath}/webjars/bootstrap/3.4.1/css/bootstrap.min.css"/> <script src="${pageContext.request.contextPath}/webjars/jquery/1.11.1/jquery.min.js"></script> <script src="${pageContext.request.contextPath}/webjars/bootstrap/3.4.1/js/bootstrap.min.js"></script> <style> body { background: url('${pageContext.request.contextPath}/images/27.jpg') no-repeat center center fixed; background-size: cover; color: black; } .container { background-color: rgba(255, 255, 255, 0); padding: 20px; border-radius: 40px; margin-top: 20px; } .table { background-color: rgba(255, 255, 255, 0.6); } .table thead { background-color:dodgerblue; color: white; } .btn-danger { background-color: #d9534f; border-color: #d43f3a; } .btn-upd { background-color: sandybrown; border-color: sandybrown; color: white; } .btn-success { background-color: dodgerblue; border-color: darkgrey; } </style> </head> <body> <div class="container"> <div class="row"> <div class="col-md-8 col-md-offset-2"> <h2 class="text-center">宠物列表</h2> <div class="text-right"> <span id="current-time" class="text-muted"></span> </div> </div> <div class="col-md-8 col-md-offset-2"> <table class="table table-bordered table-striped"> <thead> <tr class="text-center text-danger bg-success"> <td colspan="7">宠物信息列表</td> </tr> <tr class="text-center"> <td>宠物编号</td> <td>宠物名称</td> <td>宠物年龄</td> <td>宠物种类</td> <td>宠物描述</td> <td>操作</td> </tr> </thead> <tbody> <c:forEach var="pet" items="${petList}"> <tr class="text-center"> <td>${pet.id}</td> <td>${pet.name}</td> <td>${pet.age}</td> <td>${pet.type}</td> <td>${pet.description}</td> <td> <a class="btn btn-danger btn-sm" οnclick="delRow(${pet.id})">删除</a> <a class="btn btn-upd btn-sm" οnclick="updRow(${pet.id})">修改</a> </td> </tr> </c:forEach> </tbody> </table> </div> <div class="col-md-8 col-md-offset-2 text-center"> <a type="button" class="btn btn-success" href="${pageContext.request.contextPath}/goAdd">添加宠物</a> </div> </div> </div> <script> function updRow(id) { location.href = "${pageContext.request.contextPath}/upd?id=" + id; } function delRow(id) { if (confirm("你确定要删除这条宠物信息么?")) { location.href = "${pageContext.request.contextPath}/del?id=" + id; } } // 显示当前时间 function updateTime() { const now = new Date(); const timeElement = document.getElementById('current-time'); timeElement.textContent = now.toLocaleString(); } // 每秒更新一次时间 setInterval(updateTime, 1000); updateTime(); // 初始化时间 </script> </body> </html>
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 7.3 add.jsp 宠物信息列表–添加宠物<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <html> <head> <title>宠物信息列表--添加宠物</title> <link rel="stylesheet" href="${pageContext.request.contextPath}/webjars/bootstrap/3.4.1/css/bootstrap.min.css"/> <script src="${pageContext.request.contextPath}/webjars/jquery/1.11.1/jquery.min.js"></script> <script src="${pageContext.request.contextPath}/webjars/bootstrap/3.4.1/js/bootstrap.min.js"></script> <style> body { background: url('${pageContext.request.contextPath}/images/27.jpg') no-repeat center center fixed; background-size: cover; color: black; } .container { background-color: rgba( 0, 0 , 0 , 0); padding: 20px; border-radius: 10px; margin-top: 20px; } .form-control { background-color: rgba(255, 255, 255, 0.6); /* 更高的透明度 */ } .btn-success { background-color: #5cb85c; border-color: #4cae4c; } </style> </head> <body> <div class="container"> <div class="row"> <div class="col-md-6 col-md-offset-3"> <h3 class="text-center">添加宠物</h3> <div class="text-right"> <span id="current-time" class="text-muted"></span> </div> <form name="register" id="register" method="post" action="${pageContext.request.contextPath}/add" class="form-horizontal"> <div class="form-group"> <label for="name" class="col-sm-2 control-label">名字:</label> <div class="col-sm-9"> <input type="text" name="name" id="name" class="form-control" required> </div> </div> <div class="form-group"> <label for="age" class="col-sm-2 control-label">年龄:</label> <div class="col-sm-9"> <input type="number" name="age" id="age" class="form-control" required> </div> </div> <div class="form-group"> <label for="type" class="col-sm-2 control-label">种类:</label> <div class="col-sm-9"> <input type="text" name="type" id="type" class="form-control" required> </div> </div> <div class="form-group"> <label for="description" class="col-sm-2 control-label">描述:</label> <div class="col-sm-9"> <input type="text" name="description" id="description" class="form-control" required> </div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <input type="submit" name="btnRegister" id="btnRegister" value="添加" class="btn btn-success"/> </div> </div> </form> </div> </div> </div> <script> // 显示当前时间 function updateTime() { const now = new Date(); const timeElement = document.getElementById('current-time'); timeElement.textContent = now.toLocaleString(); } // 每秒更新一次时间 setInterval(updateTime, 1000); updateTime(); // 初始化时间 </script> </body> </html>
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 7.4 upd.jsp 宠物信息列表–更新宠物<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <html> <head> <title>宠物信息列表--修改宠物</title> <link rel="stylesheet" href="${pageContext.request.contextPath}/webjars/bootstrap/3.4.1/css/bootstrap.min.css"/> <script src="${pageContext.request.contextPath}/webjars/jquery/1.11.1/jquery.min.js"></script> <script src="${pageContext.request.contextPath}/webjars/bootstrap/3.4.1/js/bootstrap.min.js"></script> <style> body { background: url('${pageContext.request.contextPath}/images/27.jpg') no-repeat center center fixed; background-size: cover; color: black; } .container { background-color: rgba(0, 0, 0, 0); padding: 20px; border-radius: 10px; margin-top: 20px; } .form-control { background-color: rgba(255, 255, 255, 0.6); /* 更高的透明度 */ } .btn-success { background-color: #5cb85c; border-color: #4cae4c; } </style> </head> <body> <div class="container"> <div class="row"> <div class="col-md-6 col-md-offset-3"> <h3 class="text-center">修改宠物</h3> <div class="text-right"> <span id="current-time" class="text-muted"></span> </div> <form name="update" id="update" method="post" action="${pageContext.request.contextPath}/update" class="form-horizontal"> <input type="hidden" name="id" value="${pet.id}"> <div class="form-group"> <label for="name" class="col-sm-2 control-label">名称:</label> <div class="col-sm-10"> <input type="text" name="name" id="name" class="form-control" value="${pet.name}" required> </div> </div> <div class="form-group"> <label for="age" class="col-sm-2 control-label">年龄:</label> <div class="col-sm-10"> <input type="number" name="age" id="age" class="form-control" value="${pet.age}" required> </div> </div> <div class="form-group"> <label for="type" class="col-sm-2 control-label">种类:</label> <div class="col-sm-10"> <input type="text" name="type" id="type" class="form-control" value="${pet.type}" required> </div> </div> <div class="form-group"> <label for="description" class="col-sm-2 control-label">描述:</label> <div class="col-sm-10"> <input type="text" name="description" id="description" class="form-control" value="${pet.description}" required> </div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <input type="submit" name="btnUpdate" id="btnUpdate" value="更新" class="btn btn-success"/> </div> </div> </form> </div> </div> </div> <script> // 显示当前时间 function updateTime() { const now = new Date(); const timeElement = document.getElementById('current-time'); timeElement.textContent = now.toLocaleString(); } // 每秒更新一次时间 setInterval(updateTime, 1000); updateTime(); // 初始化时间 </script> </body> </html>
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
相关知识
基于JSP+Servlet+Mysql的宠物管理系统(简单增删改查)
基于SSM的宠物店销售系统设计与实现
基于SSM的体育赛事管理系统的设计与实现 (含源码+sql+视频导入教程+论文+PPT)
使用JDBC实现简单的宠物系统(增删改查)
基于Springboot+vue的宠物之家领养救助管理系统的设计与实现
基于SSM框架的在线宠物领养平台的设计与实现
SSM宠物领养系统非功能需求分析
基于SSM框架的宠物管理平台的设计与实现(源码+开题)
宠物商城+ssm框架+jsp页面+mysql数据库
宠物用品在线交易系统(chongwuyongpin),基于java的毕业设计
网址: 使用SSM框架实现宠物信息列表的增删改查 https://m.mcbbbk.com/newsview682491.html
上一篇: 完美世界国际版妖精练级及宠物的攻 |
下一篇: 原本娇小玲珑的宠物猪 养成了30 |