4Spring+MyBatis增删改查
Posted 红酒人生
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了4Spring+MyBatis增删改查相关的知识,希望对你有一定的参考价值。
0、oracle数据库脚本
1 create table userinfo 2 (id number(4), 3 name varchar2(50), 4 password varchar2(20 5 telephone varchar2(15), 6 isadmin varchar2(5)); 7 8 --4.2 用户表序列 9 create sequence seq_userinfo; 10 alter table userinfo add constraint pk_userinfo_id primary key(id); 11 12 insert into userinfo values(seq_userinfo.nextval,\'holly\',\'123\',\'134518024 13 \',\'是\'); 14 commit;
1、创建如下项目结构
2、在com.bean包下创建UserInfo.java
1 package com.bean; 2 3 public class UserInfo { 4 private Integer id; 5 private String name; 6 private String password; 7 private String telephone; 8 private String isadmin; 9 10 public UserInfo() { 11 12 } 13 public UserInfo(Integer id, String name, String password, String telephone, 14 String isadmin) { 15 this.id = id; 16 this.name = name; 17 this.password = password; 18 this.telephone = telephone; 19 this.isadmin = isadmin; 20 } 21 public Integer getId() { 22 return id; 23 } 24 public void setId(Integer id) { 25 this.id = id; 26 } 27 public String getName() { 28 return name; 29 } 30 public void setName(String name) { 31 this.name = name; 32 } 33 public String getPassword() { 34 return password; 35 } 36 public void setPassword(String password) { 37 this.password = password; 38 } 39 public String getTelephone() { 40 return telephone; 41 } 42 public void setTelephone(String telephone) { 43 this.telephone = telephone; 44 } 45 public String getIsadmin() { 46 return isadmin; 47 } 48 public void setIsadmin(String isadmin) { 49 this.isadmin = isadmin; 50 } 51 @Override 52 public String toString() { 53 return "UserInfo [id=" + id + ", isadmin=" + isadmin + ", name=" + name 54 + ", password=" + password + ", telephone=" + telephone + "]"; 55 } 56 57 58 }
3、在com.dao包下创建UserInfoDao.java
1 package com.dao; 2 3 import java.util.List; 4 5 import com.bean.UserInfo; 6 /** 7 * mybatis映射文件的dao接口 8 * @author pc 9 * 10 */ 11 public interface UserInfoDao { 12 public List<UserInfo> findAll(); //查所有 13 public UserInfo findUser(UserInfo user);//条件查询 14 public void insertUser(UserInfo user); //插入对象 15 public void updateUser(UserInfo user); //修改对象 16 public void deleteUser(int id); //条件删除 17 18 }
4、在com.dao包下创建UserInfoDao.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > 3 <mapper namespace="com.dao.UserInfoDao"> 4 <select id="findAll" resultType="user"> 5 select * from userinfo 6 </select> 7 <select id="findUser" resultType="user" parameterType="user"> 8 select * from userinfo where name=#{name} and password=#{password} 9 </select> 10 <insert id="insertUser" parameterType="user"> 11 insert into userinfo values(seq_userinfo.nextval,#{name},#{password},#{telephone},#{isadmin}) 12 </insert> 13 <update id="updateUser" parameterType="user"> 14 update userinfo set name=#{name},password=#{password},telephone=#{telephone},isadmin=#{isadmin} where id=#{id} 15 </update> 16 17 <delete id="deleteUser" parameterType="int"> 18 delete from userinfo where id=#{id} 19 </delete> 20 </mapper>
5、在src包下创建mybatis-config.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd" > 3 <configuration> 4 <!-- mybatis映射文件的别名配置 --> 5 <typeAliases> 6 <typeAlias type="com.bean.UserInfo" alias="user"/> 7 </typeAliases> 8 9 <!-- 注册mybatis的映射文件 --> 10 <mappers> 11 <mapper resource="com/dao/UserInfoDao.xml"/> 12 </mappers> 13 </configuration>
6、在com.dao.impl包下创建UserInfoDaoImpl.java
1 package com.dao.impl; 2 3 import java.util.List; 4 5 import org.mybatis.spring.SqlSessionTemplate; 6 7 import com.bean.UserInfo; 8 import com.dao.UserInfoDao; 9 /** 10 * 数据dao接口实现类 11 * @author pc 12 * 13 */ 14 public class UserInfoDaoImpl implements UserInfoDao { 15 private SqlSessionTemplate sqlSession; 16 /** 17 * 根据id删除 18 */ 19 public void deleteUser(int id) { 20 //获取代理对象 21 UserInfoDao dao=sqlSession.getMapper(UserInfoDao.class); 22 try { 23 dao.deleteUser(id); //调用代理对象映射的dao接口删除 24 sqlSession.commit(); //提交事务 25 System.out.println("删除成功"); 26 } catch (Exception e) { 27 System.out.println("删除失败"); 28 e.printStackTrace(); 29 } 30 } 31 /** 32 * 查询所有 33 */ 34 public List<UserInfo> findAll() { 35 //获取代理对象 36 UserInfoDao dao=sqlSession.getMapper(UserInfoDao.class); 37 List<UserInfo> list=null; 38 try { 39 list=dao.findAll(); 40 System.out.println("查询所有成功"); 41 } catch (Exception e) { 42 System.out.println("查询所有失败"); 43 e.printStackTrace(); 44 } 45 return list; 46 } 47 48 /** 49 * 条件查询 50 */ 51 public UserInfo findUser(UserInfo user) { 52 //获取代理对象 53 UserInfoDao dao=sqlSession.getMapper(UserInfoDao.class); 54 UserInfo userinfo=null; 55 try { 56 if(user!=null){ 57 userinfo=dao.findUser(user); 58 System.out.println("条件查询成功"); 59 }else{ 60 System.out.println("条件查询参数为空"); 61 } 62 } catch (Exception e) { 63 System.out.println("条件查询失败"); 64 e.printStackTrace(); 65 } 66 return userinfo; 67 } 68 /** 69 *添加 70 */ 71 public void insertUser(UserInfo user) { 72 //获取代理对象 73 UserInfoDao dao=sqlSession.getMapper(UserInfoDao.class); 74 try { 75 if(user!=null){ 76 dao.insertUser(user); 77 sqlSession.commit(); //提交事务 78 System.out.println("添加成功"); 79 }else{ 80 System.out.println("添加参数为空"); 81 } 82 } catch (Exception e) { 83 System.out.println("添加失败"); 84 e.printStackTrace(); 85 } 86 87 } 88 /** 89 * 修改对象 90 */ 91 public void updateUser(UserInfo user) { 92 //获取代理对象 93 UserInfoDao dao=sqlSession.getMapper(UserInfoDao.class); 94 try { 95 if(user!=null){ 96 dao.updateUser(user); 97 sqlSession.commit(); //提交事务 98 System.out.println("修改成功"); 99 }else{ 100 System.out.println("修改参数为空"); 101 } 102 } catch (Exception e) { 103 System.out.println("修改失败"); 104 e.printStackTrace(); 105 } 106 } 107 public SqlSessionTemplate getSqlSession() { 108 return sqlSession; 109 } 110 public void setSqlSession(SqlSessionTemplate sqlSession) { 111 this.sqlSession = sqlSession; 112 } 113 114 115 }
7、在com.service包下创建UserInfoService.java
1 package com.service; 2 3 import java.util.List; 4 5 import com.bean.UserInfo; 6 /** 7 * 服务接口 8 * @author pc 9 * 10 */ 11 public interface UserInfoService { 12 public List<UserInfo> findAll(); //查所有 13 public UserInfo findUser(UserInfo user);//条件查询 14 public void insertUser(UserInfo user); //插入对象 15 public void updateUser(UserInfo user); //修改对象 16 public void deleteUser(int id); //条件删除 17 18 19 }
8、在com.service.impl包下创建UserInfoServiceImpl.java
1 package com.service.impl; 2 3 import java.util.List; 4 5 import com.bean.UserInfo; 6 import com.dao.UserInfoDao; 7 import com.dao.impl.UserInfoDaoImpl; 8 import com.service.UserInfoService; 9 10 public class UserInfoServiceImpl implements UserInfoService{ 11 //引入数据层接口 12 UserInfoDao dao=new UserInfoDaoImpl(); 13 /** 14 * 删除 15 */ 16 public void deleteUser(int id) { 17 dao.deleteUser(id); 18 } 19 /** 20 * 查所有 21 */ 22 public List<UserInfo> findAll() { 23 return dao.findAll(); 24 } 25 /** 26 * 条件查询 27 */ 28 public UserInfo findUser(UserInfo user) { 29 return dao.findUser(user); 30 } 31 /** 32 * 插入对象 33 */ 34 public void insertUser(UserInfo user) { 35 dao.insertUser(user); 36 } 37 /** 38 * 修改对象 39 */ 40 public void updateUser(UserInfo user) { 41 dao.updateUser(user); 42 } 43 44 45 public UserInfoDao getDao() { 46 return dao; 47 } 48 public void setDao(UserInfoDao dao) { 49 this.dao = dao; 50 } 51 52 }
9、在src下创建applicationContext.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd "> 3 <!-- 1.配置数据源 --> 4 <bean id="oracleDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 5 <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/> 6 <property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:orcl"/> 7 <property name="username" value="scott"/> 8 <property name="password" value="tiger"/> 9 </bean> 10 11 <!--2.在sqlSessionFactory中注入数据源 --> 12 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 13 <!-- 注入数据源 --> 14 <property name="dataSource" ref="oracleDataSource"/> 15 16 <!-- 引用mybatis的主配置文件,(注册dao映射文件) --> 17 <property name="configLocation"> 18 <value>classpath:mybatis-config.xml</value> 19 </property> 20 </bean> 21 22 <!-- 3.用构造获取sqlSessionTemplate --> 23 <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate"> 24 <constructor-arg ref="sqlSessionFactory"/> 25 </bean> 26 27 <!-- 4.dao注入sqlSesson --> 28 <bean id="userdao" class="com.dao.impl.UserInfoDaoImpl"> 29 <property name="sqlSession" ref="sqlSessionTemplate"mybatis怎么进行增删改查