7Spring+Struts2+MyBaits(无映射接口实现类)
Posted 红酒人生
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了7Spring+Struts2+MyBaits(无映射接口实现类)相关的知识,希望对你有一定的参考价值。
1、创建userinfo.sql数据库脚本
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;
2、创建如下项目结构
3、在src的com.bean包下创建UserInfo.java
1 package com.bean; 2 /** 3 * 用户信息表 4 * @author Holly老师 5 * 6 */ 7 public class UserInfo { 8 private Integer id;//编号 9 private String name; //姓名 10 private String password; //密码 11 private String telephone; //电话 12 private String isadmin; //是否是管理员 13 14 public UserInfo() { 15 16 } 17 public UserInfo(Integer id, String name, String password, String telephone, 18 String isadmin) { 19 this.id = id; 20 this.name = name; 21 this.password = password; 22 this.telephone = telephone; 23 this.isadmin = isadmin; 24 } 25 public Integer getId() { 26 return id; 27 } 28 public void setId(Integer id) { 29 this.id = id; 30 } 31 public String getName() { 32 return name; 33 } 34 public void setName(String name) { 35 this.name = name; 36 } 37 public String getPassword() { 38 return password; 39 } 40 public void setPassword(String password) { 41 this.password = password; 42 } 43 public String getTelephone() { 44 return telephone; 45 } 46 public void setTelephone(String telephone) { 47 this.telephone = telephone; 48 } 49 public String getIsadmin() { 50 return isadmin; 51 } 52 public void setIsadmin(String isadmin) { 53 this.isadmin = isadmin; 54 } 55 @Override 56 public String toString() { 57 return "UserInfo [id=" + id + ", isadmin=" + isadmin + ", name=" + name 58 + ", password=" + password + ", telephone=" + telephone + "]"; 59 } 60 61 62 }
4、在src的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 Holly老师 9 * 10 */ 11 public interface UserInfoDao { 12 /** 13 * 1.根据对象去查询对象 14 * @return 15 */ 16 public UserInfo findUser(UserInfo userinfo); 17 /** 18 * 2.查询所有 19 * @return 20 */ 21 public List<UserInfo> findAll(); 22 /** 23 * 3.根据id查询 24 */ 25 public UserInfo findById(int id); 26 /** 27 * 4.保存对象 28 * @param userinfo 29 */ 30 public void saveObj(UserInfo userinfo); 31 /** 32 * 5.修改对象 33 */ 34 public void updateObj(UserInfo userinfo); 35 /** 36 * 6.删除对象 37 * @param id 38 */ 39 public void deleteObj(int id); 40 41 42 43 }
5、在src的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 <!-- 5 <resultMap type="userinfo" id="userMap"> 6 <id property="id" column="id"/> 7 <result property="name" column="name"/> 8 <result property="password" column="password"/> 9 <result property="telephone" column="telephone"/> 10 <result property="isadmin" column="isadmin"/> 11 </resultMap> 12 --> 13 <!--1.根据对象查询对象 --> 14 <select id="findUser" parameterType="userinfo" resultType="userinfo"> 15 select * from userinfo where name=#{name} and password=#{password} 16 </select> 17 18 <!-- 2.查询所有 --> 19 <select id="findAll" resultType="userinfo"> 20 select * from userinfo 21 </select> 22 23 <!-- 3.根据id查询 --> 24 <select id="findById" parameterType="int" resultType="userinfo"> 25 select * from userinfo where id=#{id} 26 </select> 27 28 <!-- 4.根据id删除 --> 29 <delete id="deleteObj" parameterType="int"> 30 delete from userinfo where id=#{id} 31 </delete> 32 33 <!-- 5.修改 --> 34 <update id="updateObj" parameterType="userinfo"> 35 update userinfo set name=#{name},password=#{password},telephone=#{telephone},isadmin=#{isadmin} where id=#{id} 36 </update> 37 38 <!-- 6.保存 --> 39 <insert id="saveObj" parameterType="userinfo"> 40 insert into userinfo values(seq_userinfo.nextval,#{name},#{password},#{telephone},#{isadmin}) 41 </insert> 42 43 44 </mapper>
6、在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="userinfo"/> 7 </typeAliases> 8 9 <!-- 注册mybatis的映射文件 --> 10 <mappers> 11 <mapper resource="com/dao/UserInfoDao.xml"/> 12 </mappers> 13 </configuration>
7、在src下com.action包下创建UserInfoAction.java
1 package com.action; 2 3 import java.util.List; 4 5 import javax.servlet.http.HttpServletRequest; 6 7 import org.apache.struts2.ServletActionContext; 8 9 import com.bean.UserInfo; 10 import com.dao.UserInfoDao; 11 import com.opensymphony.xwork2.ActionSupport; 12 /** 13 * 14 * @author pc 15 * ActionSupport可以最数据校验 16 */ 17 public class UserInfoAction extends ActionSupport { 18 //action中引用实体类和页面上一一对应 19 //表单name属性值里的对象 20 private UserInfo user; 21 22 23 /*注入dao*/ 24 private UserInfoDao dao; 25 /** 26 * 1.登录 27 * @return 28 */ 29 public String login(){ 30 System.out.println("action:"+user.getName()); 31 UserInfo userinfo=dao.findUser(user); 32 if(userinfo!=null){ 33 System.out.println("action查询"); 34 return "success"; 35 }else{ 36 return "error"; 37 } 38 } 39 /** 40 * 2.查询所有 41 * @return 42 */ 43 public String findAll(){ 44 List<UserInfo> list=dao.findAll(); 45 if(list!=null){ 46 System.out.println("action查询所有"); 47 HttpServletRequest request=ServletActionContext.getRequest(); 48 request.setAttribute("list", list); 49 return "success"; 50 }else{ 51 return "error"; 52 } 53 } 54 /** 55 * 3.根据id删除 56 * @return 57 */ 58 public String deleteObj(){ 59 System.out.println("id:"+user.getId()); 60 try { 61 dao.deleteObj(user.getId()); 62 System.out.println("删除成功"); 63 return "success"; 64 } catch (Exception e) { 65 System.out.println("删除失败"); 66 e.printStackTrace(); 67 return "error"; 68 } 69 } 70 71 /** 72 * 4.根据id查询 73 * @return 74 */ 75 public String findById(){ 76 System.out.println("id:"+user.getId()); 77 UserInfo userinfo=dao.findById(user.getId()); 78 if(userinfo!=null){ 79 System.out.println("根据id查询到"); 80 HttpServletRequest request=ServletActionContext.getRequest(); 81 request.setAttribute("uv", userinfo); 82 return "success"; 83 }else{ 84 return "error"; 85 } 86 } 87 /** 88 * 5.修改 89 * @return 90 */ 91 public String updateObj(){ 92 System.out.println("action:"+user); 93 try { 94 dao.updateObj(user); 95 System.out.println("修改成功"); 96 return "success"; 97 } catch (Exception e) { 98 System.out.println("修改失败"); 99 e.printStackTrace(); 100 return "error"; 101 } 102 } 103 /** 104 * 6.添加 105 * @return 106 */ 107 public String saveObj(){ 108 System.out.println("action:"+user); 109 try { 110 dao.saveObj(user); 111 System.out.println("save success"); 112 return "success"; 113 } catch (Exception e) { 114 System.out.println("save fail"); 115 e.printStackTrace(); 116 return "error"; 117 } 118 } 119 120 public UserInfo getUser() { 121 return user; 122 } 123 124 public void setUser(UserInfo user) { 125 this.user = user; 126 } 127 128 public UserInfoDao getDao() { 129 return dao; 130 } 131 public void setDao(UserInfoDao dao) { 132 this.dao = dao; 133 } 134 }
8、在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="holly"/> 8 <property name="password" value="sys"/> 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 23 24 <!-- 4.dao注入sqlSesson --> 25 <bean id="userdao" class="org.mybatis.spring.mapper.MapperFactoryBean"> 26 <property name="mapperInterface" value="com.dao.UserInfoDao"/> 27 <property name="sqlSessionFactory" ref="sqlSessionFactory"/> 28 </bean> 29 30 31 32 <!-- 6.在Action中注入service --> 33 <bean id="userAction" class="com.action.UserInfoAction"> 34 <property name="dao" ref="userdao"/> 35 </bean> 36 </beans>
9、在src下创建struts.xml
8Spring+Struts2+MyBaits(Spring注解+jdbc属性文件+log4j属性文件)