2、设置加载springMVC配置文件时,设置同时加载所有文件 --- 【文件的命名受限】
MySpringIbatis
配置applicationContext.xml
com.mysql.jdbc.Driver
root
123
jdbc:mysql://localhost:3306/test
sqlMapConfig.xml
配置sqlMapconfig.xml
insert into ibatis (id,name) values (#id#,#name#)
delete from ibatis where id=#value#
update ibatis set name=#name# where id=#id#
package com.share.bean;
public class Ibatis {
private String id;
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Ibatis() {
}
public Ibatis(String id, String name) {
super();
this.id = id;
this.name = name;
}
}
dao
package com.share.dao;
import java.util.List;
import com.share.bean.Ibatis;
public interface Dao {
/**获取全部的数据*/
public List getList();
/**根据name查询数据*/
public Ibatis getByName(String name);
/**根据id查询数据*/
public Ibatis getById(String id);
/**插入数据*/
public void insert(Ibatis ibatis);
/**删除数据*/
public void delete(String id);
/**更新*/
public void update(Ibatis ibatis);
}
package com.share.dao;
import java.util.List;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import com.share.bean.Ibatis;
public class DaoImp extends SqlMapClientDaoSupport implements Dao {
public void delete(String id) {
getSqlMapClientTemplate().delete("deleteUsers", id);
}
public Ibatis getById(String id) {
return (Ibatis)getSqlMapClientTemplate().queryForObject("getUsersById",id);
}
public Ibatis getByName(String name) {
return (Ibatis)getSqlMapClientTemplate().queryForObject("getUsersByName",name);
}
@SuppressWarnings("unchecked")
public List getList() {
return getSqlMapClientTemplate().queryForList("getAllUsers",null);
}
public void insert(Ibatis ibatis) {
getSqlMapClientTemplate().insert("insertUsers",ibatis);
}
public void update(Ibatis ibatis) {
getSqlMapClientTemplate().update("updateUsers", ibatis);
}
}
test
package com.share.test;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.share.bean.Ibatis;
import com.share.dao.DaoImp;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
DaoImp dao = (DaoImp) context.getBean("DaoImp");
dao.insert(new Ibatis("100001","tangx"));
// Ibatis ibatis3 = dao.getById("4");
// ibatis3.setName("tangxin");
// dao.update(ibatis3);
// dao.delete("4");
System.out.println("获得全查询列表");
List result = new ArrayList();
result = dao.getList();
for (Iterator iter = result.iterator(); iter.hasNext();) {
Ibatis element = (Ibatis) iter.next();
System.out.println(element.getName());
}
}
}
配置web.xml
SpringMVC_MyBatis_One
index.html
index.htm
index.jsp
default.html
default.htm
default.jsp
Dispatcher
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:application.xml
Dispatcher
*.do
classpath:com/share/ibatis/MySqlMapConfig.xml
upController
nextController
配置MySqlMapConfig.xml
配置Tx.xml
controller
package com.share.controller;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
import com.share.dao.Dao;
import com.share.user.MyData;
import com.share.user.User;
public class UpController extends AbstractController {
private String successView;
private String failView;
public String getSuccessView() {
return successView;
}
public void setSuccessView(String successView) {
this.successView = successView;
}
public String getFailView() {
return failView;
}
public void setFailView(String failView) {
this.failView = failView;
}
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response) throws Exception {
ApplicationContext context=new ClassPathXmlApplicationContext("application.xml");
Dao dao = (Dao)context.getBean("Tangxin");
System.out.println("ok?");
int current=Integer.parseInt(request.getParameter("current"));
int size=Integer.parseInt(request.getParameter("size"));
System.out.println(current+" "+size);
List list=dao.Up(new MyData(current,size));
Map model=new HashMap();
if(list!=null){
model.put("success", list);
}else{
model.put("error", "用户名或密码错误");
}
return new ModelAndView(getSuccessView(),model);
}
}
package com.share.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
public class NextController extends AbstractController{
private String ok;
private String no;
public String getOk() {
return ok;
}
public void setOk(String ok) {
this.ok = ok;
}
public String getNo() {
return no;
}
public void setNo(String no) {
this.no = no;
}
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
// TODO Auto-generated method stub
return null;
}
}
package com.share.dao;
import java.util.List;
import com.share.user.MyData;
import com.share.user.User;
public interface Dao {
public List Up(MyData data);
public List next(MyData data);
}
package com.share.dao;
import java.util.List;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import com.share.user.MyData;
import com.share.user.User;
public class DaoImpl extends SqlMapClientDaoSupport implements Dao{
@SuppressWarnings("unchecked")
@Override
public List Up(MyData data) {
// TODO Auto-generated method stub
return getSqlMapClientTemplate().queryForList("uppage",data); }
@SuppressWarnings("unchecked")
@Override
public List next(MyData data) {
// TODO Auto-generated method stub
return getSqlMapClientTemplate().queryForList("nextpage",data);
}
}
user
package com.share.user;
public class User {
public String name;
public String pwd;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
}
package com.share.user;
public class MyData {
private int current_page;
private int size;
public int getCurrent_page() {
return current_page;
}
public void setCurrent_page(int current_page) {
this.current_page = current_page;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public MyData() {
}
public MyData(int current_page, int size) {
this.current_page = current_page;
this.size = size;
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
Insert title here
用户名 密码
${user.name }
${user.pwd }
上一页
下一页
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
${error}