能夠對數據庫進行增、刪、改、查操作使用springmvc框架進行搭建。
創建Stu.java文件,內容有:
1 public class Stu { 2 //學生的相關屬性 3 private Integer id; //學號 4 private String name; //姓名 5 private Integer age; //年齡 6 private String addr; //居住地址 7 //生成相應的set和get方法 8 public Integer getId() { 9 return id; 10 } 11 public void setId(Integer id) { 12 this.id = id; 13 } 14 public String getName() { 15 return name; 16 } 17 public void setName(String name) { 18 this.name = name; 19 } 20 public Integer getAge() { 21 return age; 22 } 23 public void setAge(Integer age) { 24 this.age = age; 25 } 26 public String getAddr() { 27 return addr; 28 } 29 public void setAddr(String addr) { 30 this.addr = addr; 31 } 32 //重寫toString方法 33 @Override 34 public String toString() { 35 return "Stu [id=" + id + ", name=" + name + ", age=" + age + ", addr=" + addr + "]"; 36 }
以及一些有關框架的配置文件mybatis-config.xml、applicationContext.xml、springmvc-config.xml
1 applicationContext.xml 2 <?xml version="1.0" encoding="UTF-8"?> 3 <beans xmlns="http://www.springframework.org/schema/beans" 4 xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" 5 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 6 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 7 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 8 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd 9 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd 10 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> 11 12 <!-- 1.加載jdbc.properties文件的位置 --> 13 <context:property-placeholder location="classpath:jdbc.properties"/> 14 15 <!-- 2.配置druid連接池 ,id是固定值,class是druid連接池類的全路徑 --> 16 <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> 17 <!-- 配置連接數據庫的基本信息 --> 18 <property name="driverClassName" value="${db.driverClassName}"></property> 19 <property name="url" value="${db.url}"></property> 20 <property name="username" value="${db.username}"></property> 21 <property name="password" value="${db.password}"></property> 22 </bean> 23 24 <!-- 3.整合spring和mybatis框架 25 將SqlSession等對象的創建交給Spring容器 26 id值(sqlSessionFactory)是固定值 27 --> 28 <bean id="sqlSessionFactory" 29 class="org.mybatis.spring.SqlSessionFactoryBean"> 30 <!-- 3.1.指定mybatis核心配置文件的位置 --> 31 <property name="configLocation" 32 value="classpath:mybatis/mybatis-config.xml"></property> 33 <!-- 3.2.配置連接池(數據源) ref指向連接池bean對象的id值 --> 34 <property name="dataSource" ref="dataSource"></property> 35 <!-- 3.3、掃描所有的 XxxMapper.xml映射文件,讀取其中配置的SQL語句 --> 36 <property name="mapperLocations" value="classpath:mybatis/mapper/*.xml"/> 37 </bean> 38 39 <!-- 4、定義mapper接口掃描器 40 41 --> 42 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 43 <!-- 掃描所有XxxMapper接口,將接口實例的創建交給spring容器 --> 44 <property name="basePackage" 45 value="com.tedu.dao"/> 46 </bean> 47 48 49 </beans> 50 springmvc-config.xml 51 <?xml version="1.0" encoding="UTF-8"?> 52 <beans xmlns="http://www.springframework.org/schema/beans" 53 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" 54 xmlns:context="http://www.springframework.org/schema/context" 55 xsi:schemaLocation="http://www.springframework.org/schema/mvc 56 http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd 57 http://www.springframework.org/schema/beans 58 http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 59 http://www.springframework.org/schema/context 60 http://www.springframework.org/schema/context/spring-context-4.0.xsd"> 61 62 <!-- 1.配置前端控制器放行靜態資源(html/css/js等,否則靜態資源將無法訪問) --> 63 <mvc:default-servlet-handler/> 64 65 <!-- 2.配置注解驅動,用於識別注解(比如@Controller) --> 66 <mvc:annotation-driven></mvc:annotation-driven> 67 68 <!-- 3.配置需要掃描的包:spring自動去掃描 base-package 下的類, 69 如果掃描到的類上有 @Controller、@Service、@Component等注解, 70 將會自動將類注冊為bean 71 --> 72 <context:component-scan base-package="com.tedu.controller"> 73 </context:component-scan> 74 75 <!-- 4.配置內部資源視圖解析器 76 prefix:配置路徑前綴 77 suffix:配置文件后綴 78 --> 79 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 80 <property name="prefix" value="/WEB-INF/pages/"/> 81 <property name="suffix" value=".jsp"/> 82 </bean> 83 84 85 </beans>
進行增、刪、改、查的操作控制的相關文件代碼
1 /* 2 * 負責處理用戶的所有請求 3 */ 4 @Controller 5 public class StuController { 6 7 /* 8 * /{jspName}中的jspName用於接收訪問的路徑名稱 9 * 例如:訪問路徑為http://localhost/yonghe/index 10 * 那么{}中的jspName的值就是"index",再將{}中jspName的值 11 * 傳給形參的jspName,最后作為返回值返回,表示跳轉到這個名稱 12 * 對應的jsp頁面 13 */ 14 @RequestMapping("/{page}") 15 public String page(@PathVariable String page) { 16 return page; 17 } 18 19 /* 20 * 獲取StuMapper接口的子類實例 21 */ 22 @Autowired 23 StuMapper stuMapper; 24 25 /* 26 * 1.查詢所有學生信息 27 */ 28 @RequestMapping("/stuList") 29 public String StuList(Model model) { 30 //查詢所有學生信息 31 List<Stu> list=stuMapper.findAll(); 32 //將學生信息的集合存入model中,再轉發到jsp中 33 model.addAttribute("list", list); 34 //跳轉到stu_list.jsp,顯示所有學生信息 35 return "stu_list"; 36 37 } 38 39 /* 40 * 2.根據id刪除學生信息 41 */ 42 @RequestMapping("/stuDelete") 43 public String stuDelete(Integer id) { 44 //調用stuMapper的deleteById方法,根據id刪除學生信息 45 stuMapper.deleteById(id); 46 //跳轉到學生列表頁面,查詢所有學生信息 47 //return "stu_list";直接跳轉到學生列表頁面,不會顯示學生信息 48 return "forward:/stuList"; 49 } 50 51 /* 52 * 3.新增學生信息 53 */ 54 @RequestMapping("/stuAdd") 55 public String stuadd(Stu stu) { 56 //調用stuMapper的add方法 57 stuMapper.add(stu); 58 //跳轉到學生列表頁面,查詢最新所有學生信息 59 return "forward:/stuList"; 60 } 61 62 /* 63 * 4.根據id查詢學生信息 64 */ 65 @RequestMapping("/stuInfo") 66 public String stuInfo(Integer id,Model model) { 67 //調用stuMapper的findById方法,進行id查詢學生信息 68 Stu stu=stuMapper.findById(id); 69 //將學生對象存入Madel中,再帶到學生信息修改頁面 70 model.addAttribute("stu", stu); 71 //將查詢到的學生信息帶到學生信息修改頁面,進行數據的回顯 72 return "stu_update"; 73 } 74 75 /* 76 * 5.根據id修改學生信息 77 */ 78 @RequestMapping("/stuUpdate") 79 public String stuUpdate(Stu stu) { 80 //調用stuMapper的updateById方法 81 stuMapper.updateById(stu); 82 //轉發到查詢所有門店的方法,跳轉到學生列表頁面,查詢最新學生信息 83 return "redirect:/stuList"; 84 } 85 } 86 <?xml version="1.0" encoding="UTF-8"?> 87 <!DOCTYPE mapper 88 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 89 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 90 <!-- 學生表的映射文件 namespace值為對應接口的全路徑 --> 91 <mapper namespace="com.tedu.dao.StuMapper"> 92 <!-- 1.查詢所有學生信息,id值為對應接口中方法的名字 93 resultType指定將查詢的結果封裝到哪個pojo對象中 94 --> 95 <select id="findAll" resultType="com.tedu.pojo.Stu"> 96 select * from stu 97 </select> 98 99 <!-- 2.根據id刪除學生信息(id是傳過來的) --> 100 <delete id="deleteById"> 101 delete from stu where id=#{id} 102 </delete> 103 104 <!-- 3.新增學生信息(name、age、addr都是傳過來的) --> 105 <insert id="add"> 106 insert into stu value(null,#{name},#{age},#{addr}) 107 </insert> 108 109 <!-- 4.根據id查詢門店信息 --> 110 <select id="findById" resultType="com.tedu.pojo.Stu"> 111 select * from stu where id=#{id} 112 </select>
113 <!-- 根據id修改學生信息(修改后的學生name、age、addr也都是傳過來的) --> 114 <update id="updateById"> 115 update stu set name=#{name},age=#{age},addr=#{addr} 116 where id=#{id} 117 </update> 118 119 120 </mapper>
最后運行結果