主要是配置applicationContext和springmvc.xml
applicationContext.xml包括引入資料庫屬性檔案,組件掃描排除controller層的service層和dao層
建立dataSource的bean對象及其屬性,屬性時資料庫連接的屬性,
建立sqlSession的bean對象,注入資料來源,配置hibernate的屬性sql的格式化,建立表,方言;
建立交易管理的bean對象,注入sqlSession對象,
使用交易管理的註解方式:<tx:annotation-driven transationManager="transationManager"/>
springmvc.xml
組件掃描包含controller層 排除service層和dao層
配置內容協商管理bean對象 包含 媒體類型屬性和預設內容類型;
《mvc:anotation-driven》
mvc:default-servlet-handler
和視圖解析器 首碼和尾碼
實體類 ,@Entity @Table
(name = "person")
controller類@Controller service類 @Service dao類採用註解方式@Repository;
類裡採用
@Autowired
private PersonService personService;
@Autowiredpublic PersonDAO personDAO;
@Resourceprivate SessionFactory sessionFactory;
自動注入要調用 的類;
採用此類註解,設定檔裡就不用配置相應bean對象;
1、引入相應的jar包,spring的包:spring-aop spring-aspect spring-context spring-beans spring-core spring-orm spring-expression spring-tx spring-test spring-web spring-jdbc 大概11個
hibernate包:hibernate3 ,lib下required下的包:jta javaassist slf4j commons-collections dom4j antrl 及hibernate-jpa 大概8個
還有mysql-connect-java
sping依賴的包 spingsource...mchange...c3p0 springsource....commons-logging spingsource..aop.. springsource...aspect... springsource.log4j.slf4j-log4j大約7個
2
建立包及類 com.mz.wfy.domain Person.java
private String id;// 主鍵id name;// 姓名idCard;// 社會安全號碼 phone;// 手機號 address;//
以上都是string類型 封裝 和 toString 方法 get方法使用註解
@Id@Column(name = "id", nullable = false, unique = true)@GenericGenerator(name = "generator", strategy = "uuid")@GeneratedValue(generator = "generator")
@Column(name = "name", nullable = false, length = 32)
建立對應的hibernate 對應檔User.hbm.xml 使資料庫的表和實體映射;hibernate-mapping 中有class 節點(name table 屬性)下面有id (包括generator節點)property節點(name colomn屬性)
com.mz.wfy.controller PersonController.java
@SessionAttributes(value = "username")@Controller //使用該註解標誌它是一個控制器@RequestMapping(value = "/person")public class PersonController { @Autowired public PersonService personService; /** * 登入請求,失敗返回error.jsp * * @param username * @param password * @return */ @RequestMapping("/login") public String dologin(String username, String password, Map<String, Object> map) { if (username.equals("admin") && password.equals("admin")) { map.put("username", username);//存放在request請求域中 /** * 類上加上@SessionAttributes({"username"}) 同時也會存放在 session域中 * @SessionAttributes 除了可以通過屬性名稱指定需要存放到會話中的屬性外(使用的是value屬性值) * 還可以通過模型屬性的物件類型指定哪些模型屬性需要放到會話中(實際上使用的是types屬性值), */ return "frame"; } return "error"; } /** * 儲存添加的資料 * * @param person * @return */ @RequestMapping(value = "/saveperson") public String saveperson(Person person) { personService.addPerson(person); return "redirect:main"; } /** * 跳轉到添加頁面 * savepage.jsp * @return */ @RequestMapping(value = "/addperson") public String saveperson() { return "savepage"; } /** * 刪除一條資料 * * @param id * @return */ @RequestMapping(value = "/deletePersonById") public String deletePersonById(@RequestParam(value = "id") String id) { System.out.println("刪除單個"); personService.deletePersonById(id); return "redirect:main"; } /** * 跳轉到更新頁面,回顯資料 * editpage.jsp * @param id * @param model 使用的Model儲存回顯資料 * @return */ @RequestMapping(value = "/doupdate") public String doupdate(@RequestParam(value = "id") String id, Model model) { model.addAttribute("person", personService.getPersonById(id)); return "editpage"; } /** * 更新資料 * * @param person * @return */ @RequestMapping(value = "/updateperson") public String updateperson(Person person) { System.out.println(person.toString()); personService.updatePerson(person); return "redirect:main"; } /** * 查詢所有人員資訊 * * @param map 使用的是map儲存回顯資料 * @return */ @RequestMapping(value = "/main") public String mian(Map<String, Object> map) { map.put("personlist", personService.getPersons()); /*遍曆集合,查看查詢到的資料 * List<Person> lists = personService.getPersons(); * Iterator<Person> it = lists.iterator(); * while(it.hasNext()){ * Person p =it.next(); * System.out.println(p.toString()); * } */ return "main"; }}
com.mz.wfy.service PersonService
com.mz.wfy.dao PersonDao
private Session getSession() { return sessionFactory.getCurrentSession();}/** * 根據id查詢 * @param id * @return */public Person getPersonById(String id) { return (Person) this.getSession().createQuery("from Person where id=?").setParameter(0, id).uniqueResult();}/** * 添加 * @param person */public void addPerson(Person person) { this.getSession().save(person);}/** * 更新 * @param person */public void updatePerson(Person person) { this.getSession().update(person);}/** * 刪除 * @param id */public void deletePersonById(String id) { this.getSession().createQuery("delete Person where id=?").setParameter(0, id).executeUpdate();}/** * 查詢所有 * @return */@SuppressWarnings("unchecked")public List<Person> getPersons() { return this.getSession().createCriteria(Person.class).list();}
3.建立各種屬性檔案和設定檔,A jdbc.properties
jdbc.driverClass=com.mysql.jdbc.Driver; jdbc .url=jdbc:mysql://localhost:3306/sprsh?useUnicode=yes&characterEncoding=utf-8
jdbc.uername=root jdbc.password=123456
B log4j.properties .........百度搜一下就可以了,這個以後詳細研究下;
C applicationContext.xml
<context:component-scan base-package="com.mz.wfy"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/></context:component-scan>
a 引入外部屬性檔案 jdbc.properties 使用
<context:property-placeholder location=“classpath:jdbc.properties”/>
b 配置串連池,datasource資料來源bean對象
<bean id="dataSource" class="com.springframework.org.mchange.c3p0....>
<property name="driverClass" value="${ jdbc .driverClass}"/>
<property name="jdbcUrl" value="${ jdbc .url}"/>
<property name="jdbc.user" value="${ jdbc .username}"/>