登陸思路: 前臺發送一個請求,而後經過spring的本身主動注參注入username和password,將password加密後與數據庫中查找的作比較。返回是否經過。 html
這裏還使用了EasyUI的校驗控件。不得不說,使用框架真快。不需要寫那麼多繁雜的腳本代碼。 java
1.頁面代碼mysql
<form id="ff" method="post" action="UserInfo_login"> <div><label for="name">郵箱:</label> <input id="name" name="email" type="text" /></div> <div><label for="email">密碼:</label> <input id="pwd" name="passwords" type="password"/></div> </form>
2.腳本代碼spring
<script> $(function() { $('#login').dialog({ title : 'CRM後臺登陸', modal : true, closable : false, draggable:false, buttons : [ { text : '登陸', iconCls : 'icon-ok', handler : function() { var isValid = $('#ff').form('validate'); if (!isValid){ return false; }else{ $('#ff').submit(); } } } ], }); start(); }); function start() { $('#name').validatebox({ required : true, validType : 'email', missingMessage : "郵箱爲空 ", invalidMessage : "請輸入正確的郵箱" }); $('#pwd').validatebox({ required : true, missingMessage : "password爲空 " }); } </script>
3.Action層代碼sql
註解:數據庫
@Controller("userInfoAction") public class UserInfoAction implements ModelDriven<UserInfo>, SessionAware { @Autowired private UserInfoService userInfoService; private UserInfo userInfo; private String result; private Map<String, Object> session; private Object jsondata;
UserInfoService: 經過spring的本身主動注參可以取到業務類對象。json
UserInfo: 經過模型驅動,實現getModel方法。就能夠獲取頁面上的輸入控件的值,僅僅要name爲userInfo中的屬性就能夠。session
result: 是爲了轉發的,好比登陸失敗。多種狀況時。mybatis
session: session的取得是經過實現SessionAware接口,還要實現一個setSession方法。oracle
固然也有ActionContext和ServletActionContext,這樣的原理是基於IOC(控制反轉)爲Action注入參數。其原理就是反射。
jsondata: 這個是爲了轉發json數據而存在的。
簡單來講,這裏面的東西就是一個大容器。處處都是spring的本身主動注參。特別是service層,action層,數據持久層,連structs轉發也有它的身影。
mybatis+spring+structs2
public String login() { String password = userInfo.getPasswords(); // 原密碼 userInfo.setPasswords(Encrypt.md5(userInfo.getPasswords())); userInfo = userInfoService.login(userInfo); if (userInfo != null) { userInfo.setPasswords(password); // 在頁面上顯示的是未加密的密碼,便於改動 session.put(CrmConstants.USERINFO, userInfo); //存入session中 result = ""; if (userInfo.getPl() == 2) { // 老闆 result += "boss/frame"; // ${result}.jsp -> workers/frame.jsp } else if (userInfo.getPl() == 1) { // 員工 result += "workers/frame"; } else if (userInfo.getPl() == 0) { // 客戶 result += "customers/frame"; } else if (userInfo.getPl() == 3) { // 超級管理員 result += "workers/frame"; } return "login_success"; } else { return "fail"; } }
4.Service業務層代碼
@Service("userInfoService") public class UserInfoServiceImpl implements UserInfoService{ <span style="white-space:pre"> </span>@Autowired <span style="white-space:pre"> </span>private UserInfoMapper userInfoMapper;
public UserInfo login(UserInfo userInfo) { return userInfoMapper.findUser(userInfo); }
5.數據持久層
public interface UserInfoMapper { UserInfo getUserInfoById(int id); int insertUserInfo(UserInfo userInfo); UserInfo findUser(UserInfo userInfo); int updateUserInfoDate(UserInfo userInfo); int vailEmail(String email); int modify(UserInfo userInfo); int deleteUserByUid(int uid); }
UserInfoMapper.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.yc.crm.mapper.UserInfoMapper"> <resultMap id="BaseResultMap" type="com.yc.crm.entity.UserInfo"> <id column="CRM_ID" property="uid" /> <result column="EMAIL" property="email" /> <result column="PASSWORDS" property="passwords" /> <result column="CNAME" property="cname" /> <result column="PHONE" property="phone" /> <result column="SEX" property="sex" /> <result column="AGE" property="age" /> <result column="ADDRESS" property="address" /> <result column="USERLEVEL" property="level" /> <result column="PL" property="pl" /> <result column="CREATEDATE" property="createdate" /> <association property="bussiness" resultMap="com.yc.crm.mapper.BusinessMapper.BaseResultMap" /> </resultMap> <select id="findUser" parameterType="UserInfo" resultMap="BaseResultMap"> select * from crm_user_info u join crm_business b on u.bussiness_id=b.business_id where u.email=#{email} and u.passwords=#{passwords} </select> </mapper>
說明的是: 1. 數據庫不一樣。編寫的語句也不一樣。好比mysql的獲取自增主鍵ID和oracle不一樣,這個問題至少困擾了幾天。
2. 查詢的時候。因爲實體類和數據庫中的字段不一樣,好比字段爲企業ID,而實體類中屬性爲企業實體類,這個時候查詢需要使用join將business表聯合起來,不然獲取的business實體對象除了ID。其他屬性皆爲空。
3.假設在這裏使用了keyword,沒必要驚慌。可以加上"UID"來解決。加了"UID"必須大寫。
4.mybatis中的標籤要合理使用,好比update時候使用if-set標籤。if標籤的整形推斷條件,如下來幾個坑過個人樣例。
eg 1.0
<update id="modify" parameterType="UserInfo"> update crm_user_info <set> <if test="passwords!=null and passwords!=''"> passwords=#{passwords}, </if> phone=#{phone},sex=#{sex},"AGE"=#{age},userlevel=#{level},address=#{address} where email=#{email} </set> </update>
eg 1.1
<select id="find" parameterType="OrderDetail" resultMap="BaseResultMap"> select * from crm_order_detail det join crm_order de on det.o_id=de.o_id join crm_user_info cu on cu.crm_id=de.c_id join crm_gods gods on gods.g_id=det.g_id where de.e_id=#{order.worker.uid} <if test="state!=null and state>=0"> and det.state=#{state} </if> <if test="god!=null and god.name!=null and god.name!='' "> and gods.g_name like '%'||#{god.name}||'%' </if> <!--<if test="odate!=null and udate!=null"> <![CDATA[ >= ]]> and odate between to_date(#{odate},'yyyy-mm-dd') and to_date(#{udate},'yyyy-mm-dd') </if> --> <if test="order!=null and order.customer!=null and order.customer.cname!=null and order.customer.cname!=''"> and cu.cname like '%'||#{order.customer.cname}||'%' </if> </select>
2.上面的推斷條件不是一步到位,而是一步一步的推斷是否爲空。
order!=null and order.customer!=null and order.customer.cname!=null and order.customer.cname!=''
3.like查詢使用了字符拼接。使用自帶函數我沒有成功過。
至於找回password,發送請求,後臺發送一封郵件至客戶郵箱,這個技術不是很是難。
我是菜鳥,我在路上。