开发环境:System:Windows server 2003WebBrowser:IE6+、Firefox3+JavaEE Server:tomcat5.IDE:eclipse、MyEclipse 6.5Database:MySQL开发依赖库:JavaEE5、Spring 3. ...
开发环境:
System:Windows server 2003
WebBrowser:IE6+、Firefox3+
JavaEE Server:tomcat5.
IDE:eclipse、MyEclipse 6.5
Database:MySQL
开发依赖库:
JavaEE5、Spring 3.0.5、Mybatis 3.0.2、myBatis-spring-1.0.0-rc2
参考百度文库:http://wenku.baidu.com/view/34559702a6c30c2259019e4e.html
源码下载:http://download.csdn.net/detail/nosi79998/6486219
1、 首先新建一个WebProject 命名为ssi,新建项目时,使用JavaEE5的lib库。然后手动添加需要的jar包,所需jar包如下:
2、 添加spring的监听及springMVC的核心Servlet,web.
<beans
<!-- 配置DataSource数据源 --> <bean id="dataSource" > <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://192.168.1.36:3306/test"/> <property name="username" value="fssykj"/> <property name="password" value="fssykj"/> </bean> <!-- 配置SqlSessionFactoryBean --> <bean id="sqlSessionFactory" > <property name="dataSource" ref="dataSource"/> <property name="configLocation" value="classpath:mybatis.
@Mapper("mapper") public interface AccountMapper extends SqlMapper { public List<Account> getAllAccount(); public Account getAccount(); public Account getAccountById(String id); public Account getAccountByNames(String spring); @Select("select * from account where username = #{name}") public Account getAccountByName(String name); public void addAccount(Account account); public void editAccount(Account account); public void removeAccount(int id);}
private static final long serialVersionUID = -7970848646314840509L; private Integer accountId; private Integer status; private String username; private String password; private String salt; private String email; private Integer roleId; //getter、setter @Override public String toString() { return this.accountId + "#" + this.status + "#" + this.username + "#" + this.password + "#" + this.email + "#" + this.salt + "#" + this.roleId; }
<?
<beans
在这个文件放置一些全局性的配置,如handler、objectFactory、plugin、以及mappers的映射路径(由于在applicationContext-common中的SqlSessionFactoryBean有配置mapper的location,这里就不需要配置)等
9、 定义AccountDao接口及实现代码,代码如下:
public interface AccountDao<T> { public boolean addAccount(T entity) throws DataAccessException; public T getAccount(Integer id) throws DataAccessException; public List<T> getList() throws DataAccessException;}
import java.util.List;import javax.inject.Inject;import org.springframework.dao.DataAccessException;import org.springframework.stereotype.Repository;import org.springframework.beans.factory.annotation.Autowired;import com.hoo.dao.AccountDao;import com.hoo.entity.*;import com.hoo.mapper.*;import org.springframework.beans.factory.annotation.Qualifier;@SuppressWarnings("unchecked")@Repository("accountDaoImpl")public class AccountDaoImpl<T extends Account> implements AccountDao<T> { @Autowired(required=false) @Qualifier("mapper") private AccountMapper mapper; public boolean addAccount(T entity) throws DataAccessException { boolean flag=false; try{ mapper.addAccount(entity); flag=true; } catch(DataAccessException e) { flag=false; throw e; } return flag; } public T getAccount(Integer id) throws DataAccessException { T entity = null; try { entity = (T)mapper.getAccountById(String.valueOf(id)); } catch(DataAccessException e) { throw e; } return entity; } public List<T> getList() throws DataAccessException { return (List<T> )mapper.getAllAccount(); }}
package com.hoo.biz.impl;import java.util.List;import org.springframework.dao.DataAccessException;import com.hoo.biz.AccountBiz;import com.hoo.entity.*;import javax.inject.Inject;import com.hoo.dao.*;import org.springframework.stereotype.Service;import com.hoo.exception.BizException;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;@Service("accountBizImpl")public class AccountBizImpl<T extends Account> implements AccountBiz<T> { @Autowired @Qualifier("accountDaoImpl") private AccountDao<T> dao; public boolean addAccount(T entity) throws DataAccessException { if(entity==null){ throw new BizException(Account.class.getName()+"对象参数为empty!"); } return dao.addAccount(entity); } public T getAccount(Integer id) throws DataAccessException { return dao.getAccount(id); } public List getList() throws DataAccessException { return dao.getList(); }}
package com.hoo.controller;import javax.inject.Inject;import javax.servlet.http.HttpServletRequest;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.ExceptionHandler;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.beans.factory.annotation.Autowired;import com.hoo.biz.AccountBiz;import com.hoo.entity.Account;import org.springframework.beans.factory.annotation.Qualifier;@Controller("accountController")@RequestMapping("/account")public class AccountController { @Autowired @Qualifier("accountBizImpl") private AccountBiz<Account> biz; @RequestMapping("/add") public String add(@RequestParam String username, @RequestParam String password, @RequestParam String status) { Integer stat=Integer.valueOf(status); Account acc=new Account(username,password,stat); System.out.println(acc); biz.addAccount(acc); return "redirect:/account/list.do"; } @RequestMapping("/get") public String get(Integer id,Model model) { System.out.println("###ID:"+id); model.addAttribute(biz.getAccount(id)); return "/show.jsp"; } @RequestMapping("/list") public String list(Model model) { model.addAttribute("list",biz.getList()); return "/list.jsp"; } @ExceptionHandler(Exception.class) public String exception(Exception e,HttpServletRequest request) { request.setAttribute("exception", e); return "/error.jsp"; }}
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <title>My JSP 'list.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <c:forEach items="${list}" var="data"> id:${data.accountId }--name:${data.username }--password: ${data.password }<hr/> </c:forEach> </body></html>
同时还需要加入aspectjweaver.jar这个jar包;
注意的是:Jdbc的TransactionManager不支持事务隔离级别,我在整个地方加入其它的TransactionManager,增加对transaction的隔离级别都尝试失败!
也许可以用于jpa、jdo、jta这方面的东西。
请参考一下资料下载源码:
http://www.spring.net
原标题:springmvc+spring+mybatis整合实例
关键词:Spring
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:
admin#shaoqun.com
(#换成@)。