搭建一个spring-boot项目 3

来源:互联网 发布:践行五大发展理念 知乎 编辑:程序博客网 时间:2024/05/16 07:59

service:
1,代码结构
这里写图片描述
2,连接数据库
application.properties中加入,下面的配置

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/db_yk01spring.datasource.username=rootspring.datasource.password=123456spring.datasource.driver-class-name=com.mysql.jdbc.Driverspring.datasource.max-idle=10spring.datasource.max-wait=10000spring.datasource.min-idle=5spring.datasource.initial-size=5mybatis.mapperLocations=classpath:mapper/**/*.xml

事务配置:不习惯用@Transction注解
transction-config.xml

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"    xmlns:aop="http://www.springframework.org/schema/aop" 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-3.0.xsd    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">    <!-- 事务配置 -->    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">        <property name="dataSource" ref="dataSource" />    </bean>    <!-- 注解事务-->    <tx:annotation-driven transaction-manager="transactionManager" />    <!-- 通过AOP配置提供事务增强,让service包下所有Bean的所有方法拥有事务 -->    <aop:config>        <aop:pointcut id="serviceMethod" expression="execution(* com.yk01..service..*.*(..))" />        <aop:advisor pointcut-ref="serviceMethod" advice-ref="txAdvice" />    </aop:config>    <tx:advice id="txAdvice" transaction-manager="transactionManager">        <tx:attributes>           <tx:method name="get*" propagation="SUPPORTS" read-only="true" />             <tx:method name="count*" propagation="SUPPORTS" read-only="true" />             <tx:method name="find*" propagation="SUPPORTS" read-only="true" />             <tx:method name="list*" propagation="SUPPORTS" read-only="true" />             <tx:method name="select*" propagation="SUPPORTS" read-only="true" />             <tx:method name="search*" propagation="SUPPORTS" read-only="true" />             <tx:method name="*" propagation="REQUIRED"  read-only="false"/>        </tx:attributes>     </tx:advice></beans>

启动类Application.java 加上下面注解
@ImportResource(locations={“classpath*:transction-config.xml”})

3,第一个EO

package com.yk01.security.auth.entity;import java.io.Serializable;import java.util.Date;public class SysAuthEO implements Serializable{    private Integer id;    private String authMobile;    private String authPassword;    private String authState;    private Integer userId;    private Date createDatetime;    private Date updateDatetime;    public Integer getId() {        return id;    }    public void setId(Integer id) {        this.id = id;    }    public String getAuthMobile() {        return authMobile;    }    public void setAuthMobile(String authMobile) {        this.authMobile = authMobile == null ? null : authMobile.trim();    }    public String getAuthPassword() {        return authPassword;    }    public void setAuthPassword(String authPassword) {        this.authPassword = authPassword == null ? null : authPassword.trim();    }    public String getAuthState() {        return authState;    }    public void setAuthState(String authState) {        this.authState = authState == null ? null : authState.trim();    }    public Integer getUserId() {        return userId;    }    public void setUserId(Integer userId) {        this.userId = userId;    }    public Date getCreateDatetime() {        return createDatetime;    }    public void setCreateDatetime(Date createDatetime) {        this.createDatetime = createDatetime;    }    public Date getUpdateDatetime() {        return updateDatetime;    }    public void setUpdateDatetime(Date updateDatetime) {        this.updateDatetime = updateDatetime;    }    @Override    public String toString() {        return "SysAuthEO [id=" + id + ", authMobile=" + authMobile + ", authPassword=" + authPassword + ", authState="                + authState + ", userId=" + userId + ", createDatetime=" + createDatetime + ", updateDatetime="                + updateDatetime + "]";    }}

3,Dao.想要扫描到@Mapper标识的类,该类要放到Application.java所在包的子包下。写一个通用的MyMapper.java感觉跟使用工具生成Mapper.xml便利性差不多。

package com.yk01.security.auth.dao;import org.apache.ibatis.annotations.Mapper;import org.apache.ibatis.annotations.Select;import com.yk01.security.auth.entity.SysAuthEO;@Mapperpublic interface ISysAuthDao {    public int deleteByPrimaryKey(Integer id);    public int insert(SysAuthEO record);    public int insertSelective(SysAuthEO record);    public SysAuthEO selectByPrimaryKey(Integer id);    public int updateByPrimaryKeySelective(SysAuthEO record);    public int updateByPrimaryKey(SysAuthEO record);}

4,Mapper.xml,这个是通过mybatis-generator生成

<?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.yk01.security.auth.dao.ISysAuthDao">  <resultMap id="BaseResultMap" type="com.yk01.security.auth.entity.SysAuthEO">    <id column="id" jdbcType="INTEGER" property="id" />    <result column="auth_mobile" jdbcType="VARCHAR" property="authMobile" />    <result column="auth_password" jdbcType="VARCHAR" property="authPassword" />    <result column="auth_state" jdbcType="VARCHAR" property="authState" />    <result column="user_id" jdbcType="INTEGER" property="userId" />    <result column="create_datetime" jdbcType="TIMESTAMP" property="createDatetime" />    <result column="update_datetime" jdbcType="TIMESTAMP" property="updateDatetime" />  </resultMap>  <sql id="Base_Column_List">    id, auth_mobile, auth_password, auth_state, user_id, create_datetime, update_datetime  </sql>  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">    select     <include refid="Base_Column_List" />    from sys_auth    where id = #{id,jdbcType=INTEGER}  </select>  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">    delete from sys_auth    where id = #{id,jdbcType=INTEGER}  </delete>  <insert id="insert" parameterType="com.yk01.security.auth.entity.SysAuthEO" useGeneratedKeys="true" keyProperty="id">    insert into sys_auth (id, auth_mobile, auth_password,       auth_state, user_id, create_datetime,       update_datetime)    values (#{id,jdbcType=INTEGER}, #{authMobile,jdbcType=VARCHAR}, #{authPassword,jdbcType=VARCHAR},       #{authState,jdbcType=VARCHAR}, #{userId,jdbcType=INTEGER}, #{createDatetime,jdbcType=TIMESTAMP},       #{updateDatetime,jdbcType=TIMESTAMP})  </insert>  <insert id="insertSelective" parameterType="com.yk01.security.auth.entity.SysAuthEO" useGeneratedKeys="true" keyProperty="id">    insert into sys_auth    <trim prefix="(" suffix=")" suffixOverrides=",">      <if test="id != null">        id,      </if>      <if test="authMobile != null">        auth_mobile,      </if>      <if test="authPassword != null">        auth_password,      </if>      <if test="authState != null">        auth_state,      </if>      <if test="userId != null">        user_id,      </if>      <if test="createDatetime != null">        create_datetime,      </if>      <if test="updateDatetime != null">        update_datetime,      </if>    </trim>    <trim prefix="values (" suffix=")" suffixOverrides=",">      <if test="id != null">        #{id,jdbcType=INTEGER},      </if>      <if test="authMobile != null">        #{authMobile,jdbcType=VARCHAR},      </if>      <if test="authPassword != null">        #{authPassword,jdbcType=VARCHAR},      </if>      <if test="authState != null">        #{authState,jdbcType=VARCHAR},      </if>      <if test="userId != null">        #{userId,jdbcType=INTEGER},      </if>      <if test="createDatetime != null">        #{createDatetime,jdbcType=TIMESTAMP},      </if>      <if test="updateDatetime != null">        #{updateDatetime,jdbcType=TIMESTAMP},      </if>    </trim>  </insert>  <update id="updateByPrimaryKeySelective" parameterType="com.yk01.security.auth.entity.SysAuthEO">    update sys_auth    <set>      <if test="authMobile != null">        auth_mobile = #{authMobile,jdbcType=VARCHAR},      </if>      <if test="authPassword != null">        auth_password = #{authPassword,jdbcType=VARCHAR},      </if>      <if test="authState != null">        auth_state = #{authState,jdbcType=VARCHAR},      </if>      <if test="userId != null">        user_id = #{userId,jdbcType=INTEGER},      </if>      <if test="createDatetime != null">        create_datetime = #{createDatetime,jdbcType=TIMESTAMP},      </if>      <if test="updateDatetime != null">        update_datetime = #{updateDatetime,jdbcType=TIMESTAMP},      </if>    </set>    where id = #{id,jdbcType=INTEGER}  </update>  <update id="updateByPrimaryKey" parameterType="com.yk01.security.auth.entity.SysAuthEO">    update sys_auth    set auth_mobile = #{authMobile,jdbcType=VARCHAR},      auth_password = #{authPassword,jdbcType=VARCHAR},      auth_state = #{authState,jdbcType=VARCHAR},      user_id = #{userId,jdbcType=INTEGER},      create_datetime = #{createDatetime,jdbcType=TIMESTAMP},      update_datetime = #{updateDatetime,jdbcType=TIMESTAMP}    where id = #{id,jdbcType=INTEGER}  </update></mapper>
0 0
原创粉丝点击