首页 > 代码库 > SpringMVC+Mybatis整合的增删改查
SpringMVC+Mybatis整合的增删改查
本文基于 SPRING注解。本文使用Oracle数据库。
项目文件下载地址:http://download.csdn.net/detail/u010634066/8188965
项目总图:
现在lib中导入所有所需jar包:这里就不叙述了
一:在SRC下创建一个Bean包;在bean下面添加实体类,实体类对应于数据表,其属性与数据表相同或多于数据表。
/** * */ package com.szz.bean; import com.szz.base.bean.BaseObject; /** * @author Administrator * */ public class User extends BaseObject { private String ID; /** * @return the iD */ public String getID() { return ID; } /** * @param iD the iD to set */ public void setID(String iD) { ID = iD; } /** * @return the nAME */ public String getNAME() { return NAME; } /** * @param nAME the nAME to set */ public void setNAME(String nAME) { NAME = nAME; } /** * @return the pASSWORD */ public String getPASSWORD() { return PASSWORD; } /** * @param pASSWORD the pASSWORD to set */ public void setPASSWORD(String pASSWORD) { PASSWORD = pASSWORD; } private String NAME; private String PASSWORD; /* (non-Javadoc) * @see com.szz.base.bean.BaseObject#toString() */ /* (non-Javadoc) * @see java.lang.Object#toString() */ @Override public String toString() { return "User [ID=" + ID + ", NAME=" + NAME + ", PASSWORD=" + PASSWORD + "]"; } }
二、创建com.szz.dao包;里面用来定义需要对数据进行操作的实体类型DAO接口
/** * */ package com.szz.dao; import java.util.List; import com.szz.bean.User; /** * @author Administrator * */ public interface UserDao { /* * 查询 */ public List<User> selectAll(); public User findById(String id); public User findByUserName(String userName); public int countAll(); /* * 更新删除插入 */ public int insert(User user); public int update(User user); public int delete(String userName); /* //返回插入数据的ID public int findInsertUserID(User user);*/ /*//批处理 插入多条数据 public void insertUsers(List<User> users);*/ }
三、创建包com.szz.tables.xml(这样命名好像不好 定义com.szz.Mappers比较直观一点) 这个是用来写sql语句的xml文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!-- 这里定义好命名空间 --> <mapper namespace="com.szz.dao.UserDao"> <select id="selectAll" resultType="User"> select * from SM_USER </select> <select id="countAll"> select count(*) c from SM_USER </select> <select id="findById" parameterType="String" resultType="User"> select * from SM_USER where ID=#{ID} </select> <select id="findByUserName" parameterType="String" resultType="User"> select * from SM_USER where NAME=#{NAME} </select> <!-- <select id="findInsertUserID" paramterType="Srtring"> select ID FROM SM_USER NAME =#{User.NAME} </select> --> <insert id="insert" parameterType="User"> insert into SM_USER(ID,NAME,PASSWORD) VALUES(#{ID},#{NAME},#{PASSWORD}) </insert> <update id="update" parameterType="User"> update SM_USER <set> <!-- 这里要注意后面的 逗号“,” 因为有多个参数需要用逗号隔开 否则会报错 --> <if test="NAME!=null">NAME=#{NAME},</if> <if test="PASSWORD!=null">PASSWORD=#{PASSWORD}</if> </set> where ID=#{ID} </update> <delete id="delete" parameterType="String"> delete FROM SM_USER WHERE ID=#{ID} </delete> </mapper>
命名空间定义为我们需要对应的DAO接口;这里每个方法的ID都跟DAO里面的方法一一对应;
还有说明一下
parameterType="User"如果你没有在mybatis配置文件里面定义别名 这样写就会报错 你要把全类名写清楚
<typeAliases> <typeAlias type="com.szz.bean.User" alias="User"/> </typeAliases>
四、spring的配置文件 spring-context.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 启动扫描szz下所有的注解--> <context:component-scan base-package="com.szz"/> <mvc:annotation-driven ignore-default-model-on-redirect="true"/> <mvc:default-servlet-handler/> <!-- 可通过注解控制事务 <tx:annotation-driven /> --> <!-- 导入外部的资源文件 一般都会把数据源的配置文件房子properties文件里面 然后用这种方式来导入 <context:property-placeholder location="classpath:jdbc.properties"/>--> <!-- 配置DataSource数据源 配置mysql方式 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value=http://www.mamicode.com/"com.mysql.jdbc.Driver" />>
配置文件说明: 详情见http://www.blogjava.net/ldwblog/archive/2013/07/10/401418.html
<!-- 用户Dao --> <bean id="userDao" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="mapperInterface" value=http://www.mamicode.com/"com.szz.dao.UserDao" /> >
MapperScannerConfigurer
Xml代码
五、创建mybatis-config.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value=http://www.mamicode.com/"oracle.jdbc.driver.OracleDriver" />>六、创建services接口
package com.szz.service; import java.util.List; import com.szz.bean.User; public interface UserService { public List<User> getUsers(); /* * 濡傛灉ID涓虹┖灏辨壘username 濡傛灉username涓虹┖灏辨壘ID锛?閮藉~鎸夌収ID */ public User getUserInfo(String ID,String userName); public int getCount(); // public int saveUser(User user); public int insertUser(User user); public int updateUser(User user); public int deleteUser(String ID); }
七‘services接口的实现类 serviceImpl
/** * */ package com.szz.service.impl; import java.util.List; import org.mybatis.spring.SqlSessionTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.szz.bean.User; import com.szz.dao.UserDao; import com.szz.service.UserService; /** * @author Administrator * */ @Service("userService") public class UserServiceImpl implements UserService { @Autowired private UserDao userDao; /* @Autowired private SqlSessionTemplate sessionTemplate;*/ public UserDao getUserDao() { return userDao; } /** * @param userDao the userDao to set */ public void setUserDao(UserDao userDao) { this.userDao = userDao; } @Override public List<User> getUsers() { // TODO Auto-generated method stub return userDao.selectAll(); //return sessionTemplate.getMapper(UserDao.class).selectAll(); } @Override public User getUserInfo(String ID, String userName) { // TODO Auto-generated method stub if(ID!=null){ return userDao.findById(ID); } else return userDao.findByUserName(userName); } @Override public int getCount() { // TODO Auto-generated method stub return userDao.countAll(); } @Override public int insertUser(User user) { // TODO Auto-generated method stub return userDao.insert(user); } @Override public int updateUser(User user) { // TODO Auto-generated method stub return userDao.update(user); } @Override public int deleteUser(String ID) { // TODO Auto-generated method stub return userDao.delete(ID); } }
八、创建控制类controller包/**/** * */ package com.szz.action; import java.util.ArrayList; import java.util.List; import javax.servlet.http.HttpServletRequest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.servlet.ModelAndView; import com.szz.base.acion.BaseAction; import com.szz.bean.User; import com.szz.service.UserService; /** * @author Administrator * */ @Controller @RequestMapping(value=http://www.mamicode.com/"/user")>
九、web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet> <servlet-name>Dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value> classpath*:spring-context.xml </param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
十、一些页面之类的;index.jsp
<body> <form action="/SpringMvcMybatisFreeMarker/user/login" method="post"> 用户ID:<input type="text" name="id" value=http://www.mamicode.com/""/>>
user/index.jsp<body> <form action="user/insertuser" method="post"> <table> <tr> <th>ID</th> <td><input type="text" name="ID" /> </td> </tr> <tr> <tr> <th>账号</th> <td><input type="text" name="NAME" /> </td> </tr> <tr> <th>密码</th> <td><input type="text" name="PASSWORD" /></td> </tr> <tr> <td colspan="2"><input type="submit" value=http://www.mamicode.com/"添加"> >
user/edit.jsp<body> <form action="user/updateuser" method="post"> <input type="text" name="ID" value=http://www.mamicode.com/"${user.ID}" />>
访问:http://localhost:8080/SpringMvcMybatisFreeMarker/index.jsp
SpringMVC+Mybatis整合的增删改查
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。