09.事务管理、整合jpa、整合mybatis

事务管理

  • spring-boot-starter-jdbc会自动默认注入DataSourceTransactionManager
  • spring-boot-starter-data-jpa会自动默认注入JpaTransactionManager
@Service
@Transactional
public class UseDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    public int updatePhoneByIDOne(String phone,Long id){
        String sql = "update ADMIN_USER set phone=? where ID = ?";
        int i = jdbcTemplate.update(sql, new Object[]{phone, id});
        int a = 1/0;
        return i;
    }
}

整合jpa

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.username=
spring.datasource.password=
spring.datasource.url=

spring.jackson.serialization.indent-output=true
#spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.hibernate.dialect=org.hibernate.dialect.Oracle12cDialect

import javax.persistence.*;
import java.util.Date;

@Entity
@Table(name = "ADMIN_USER")
public class AdminUser {
    @Id
    @GeneratedValue(strategy = GenerationType.TABLE,generator = "pk_gen_admin_user")
    @TableGenerator(name = "pk_gen_admin_user",table = "sys_id_gen",pkColumnName = "gen_name",pkColumnValue = "user_admin_user",valueColumnName = "gen_value",allocationSize = 1)
    private Long id;
    private String name;
    private String phone;
    private String province;
    private String city;
    private String district;
//    private Date createData;
    private String company;
    private String remark;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getProvince() {
        return province;
    }

    public void setProvince(String province) {
        this.province = province;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getDistrict() {
        return district;
    }

    public void setDistrict(String district) {
        this.district = district;
    }

//    public Date getCreateData() {
//        return createData;
//    }

//    public void setCreateData(Date createData) {
//        this.createData = createData;
//    }

    public String getCompany() {
        return company;
    }

    public void setCompany(String company) {
        this.company = company;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }
}

import com.fly.entity.AdminUser;
import org.springframework.data.jpa.repository.JpaRepository;

public interface AdminUserDao extends JpaRepository<AdminUser,Long> {
}

import com.fly.dao.AdminUserDao;
import com.fly.entity.AdminUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AdminUserServiceImpl {
    @Autowired
    private AdminUserDao adminUserDao;
    public AdminUser findOne(Long id){
        return adminUserDao.findOne(id);
    }

    public AdminUser save(AdminUser adminUser){
        return adminUserDao.save(adminUser);
    }
}

整合mybatis

      <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>12.1.0.1-atlassian-hosted</version>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </dependency>
        <!--分页插件-->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.1.2</version>
        </dependency>
spring:
  datasource:
    driver-class-name: oracle.jdbc.OracleDriver
    url: 
    username: 
    password: 
    type: com.alibaba.druid.pool.DruidDataSource
    druid:
      filters: stat
      maxActive: 20
      initialSize: 1
      maxWait: 60000
      minIdle: 1
      time-between-eviction-runs-millis: 60000
      min-evictable-idle-time-millis: 300000
      validation-query: select 'x' FROM DUAL
      test-while-idle: true
      test-on-borrow: false
      test-on-return: false
      pool-prepared-statements: true
      max-open-prepared-statements: 20
mybatis:
  mapper-locations: classpath:mapper/AdminUserMapper.xml
#  config-location: classpath:mybatis/mybatis-config.xml

pagehelper:
  helper-dialect: oracle
  reasonable: true
  support-methods-arguments: true
  params: count=countSql
import com.fly.pojo.AdminUser;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@Mapper
public interface AdminUserMapper {
    List<AdminUser> findAll();
}

mybatis/mybatis-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

</configuration>

mapper/AdminUserMapper.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.fly.Mapper.AdminUserMapper">
    <resultMap id="BaseResultMap" type="com.fly.pojo.AdminUser">
        <id column="id" property="id"/>
        <result column="name" property="name"/>
        <result column="phone" property="phone"/>
        <result column="province" property="province"/>
        <result column="city" property="city"/>
        <result column="district" property="district"/>
        <result column="company" property="company"/>
        <result column="remark" property="remark"/>
        <result column="create_date" property="createDate"/>
    </resultMap>
    <select id="findAll" resultMap="BaseResultMap">
        select id,name,phone,province,city,district,company,remark,create_date from ADMIN_USER
    </select>
</mapper>
@MapperScan("com.fly.Mapper")
public class SpringDemoApp{

测试

import com.app.SpringDemoApp;
import com.fly.Mapper.AdminUserMapper;
import com.fly.pojo.AdminUser;
import com.github.pagehelper.PageHelper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.List;

@SpringBootTest(classes = SpringDemoApp.class)
@RunWith(SpringJUnit4ClassRunner.class)
public class AdminUserMapperTest {
    @Autowired
    private AdminUserMapper adminUserMapper;

    @Test
    public void test1(){
        PageHelper.startPage(1,10);
        List<AdminUser> adminUsers = adminUserMapper.findAll();
        for (AdminUser adminUser : adminUsers) {
            System.out.println(adminUser);
        }
    }
}
import com.fly.pojo.AdminUser;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface AdminUser2Mapper {
    @Select("select o.id,o.name,o.phone,o.province,o.city,o.district,o.company,o.remark,o.create_date createDate from Admin_User o")
    List<AdminUser> findAll();
    @Select("select o.id,o.name,o.phone,o.province,o.city,o.district,o.company,o.remark,o.create_date createDate from Admin_User o where o.id=#{id}")
    AdminUser findOne(@Param("id") Long id);
}
posted @ 2019-09-29 08:37  fly_bk  阅读(899)  评论(0编辑  收藏  举报