1,创建表对应的实体类
package org.jasig.cas.web.flow; import java.io.Serializable; import java.util.Date; public class LoginLog implements Serializable { private static final long serialVersionUID = 6368548238243591399L; private String id; private String username; private String ipAddr; private String operationType; private Integer errorTimes; private Date operationTime; private String systemFlag; private String requestReferer; private String userAgent; private Date createDate; private String creator; private String modifier; private Date modifyDate; public String getId() { return id; } public void setId(String id) { this.id = id == null ? null : id.trim(); } public String getUsername() { return username; } public void setUsername(String username) { this.username = username == null ? null : username.trim(); } public String getIpAddr() { return ipAddr; } public void setIpAddr(String ipAddr) { this.ipAddr = ipAddr == null ? null : ipAddr.trim(); } public String getOperationType() { return operationType; } public void setOperationType(String operationType) { this.operationType = operationType == null ? null : operationType.trim(); } public Integer getErrorTimes() { return errorTimes; } public void setErrorTimes(Integer errorTimes) { this.errorTimes = errorTimes; } public Date getOperationTime() { return operationTime; } public void setOperationTime(Date operationTime) { this.operationTime = operationTime; } public String getSystemFlag() { return systemFlag; } public void setSystemFlag(String systemFlag) { this.systemFlag = systemFlag == null ? null : systemFlag.trim(); } public String getRequestReferer() { return requestReferer; } public void setRequestReferer(String requestReferer) { this.requestReferer = requestReferer == null ? null : requestReferer.trim(); } public String getUserAgent() { return userAgent; } public void setUserAgent(String userAgent) { this.userAgent = userAgent == null ? null : userAgent.trim(); } public Date getCreateDate() { return createDate; } public void setCreateDate(Date createDate) { this.createDate = createDate; } public String getCreator() { return creator; } public void setCreator(String creator) { this.creator = creator == null ? null : creator.trim(); } public String getModifier() { return modifier; } public void setModifier(String modifier) { this.modifier = modifier == null ? null : modifier.trim(); } public Date getModifyDate() { return modifyDate; } public void setModifyDate(Date modifyDate) { this.modifyDate = modifyDate; } }
2,dao层
package org.jasig.cas.web.flow; import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource; import org.springframework.jdbc.core.simple.SimpleJdbcTemplate; import org.springframework.jdbc.support.GeneratedKeyHolder; import org.springframework.jdbc.support.KeyHolder; import javax.sql.DataSource; import javax.validation.constraints.NotNull; /** * @author myron 2019/10/12 */ public class InsertLoginLogDao { @NotNull private SimpleJdbcTemplate jdbcTemplate; @NotNull private DataSource dataSource; @NotNull private String insertSql; /** * Method to set the datasource and generate a JdbcTemplate. * * @param dataSource the datasource to use. */ public final void setDataSource(final DataSource dataSource) { this.jdbcTemplate = new SimpleJdbcTemplate(dataSource); this.dataSource = dataSource; } protected final SimpleJdbcTemplate getJdbcTemplate() { return this.jdbcTemplate; } protected final DataSource getDataSource() { return this.dataSource; } public void saveLoginLog(LoginLog loginLog) { KeyHolder keyHolder = new GeneratedKeyHolder(); getJdbcTemplate().getNamedParameterJdbcOperations().update( this.insertSql, new BeanPropertySqlParameterSource(loginLog), keyHolder ); } /** * @param insertSql The sql to set. */ public void setInsertSql(final String insertSql) { this.insertSql = insertSql; } }
3,xml文件
<bean id="insertLoginLogDao" class="org.jasig.cas.web.flow.InsertLoginLogDao"> <property name="dataSource" ref="dataSource" /> <property name="insertSql" value="insert into p_login_log (id,username,ip_addr,operation_type,error_times, operation_time,system_flag,request_referer,user_agent,create_date,creator,modifier,modify_date) values(:id,:username,:ipAddr,:operationType,:errorTimes, :operationTime,:systemFlag,:requestReferer,:userAgent,:createDate,:creator,:modifier,:modifyDate)" /> </bean>
一定要注意写法
利用SimpleJdbcTemplate进行新增,spring容器管理对象
原文:https://www.cnblogs.com/mmh760/p/11673908.html