zoukankan      html  css  js  c++  java
  • spring+mybatise注解实现

    spring+mybatise注解实现

     1 spring.jpa.database=MYSQL
     2 
     3 spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
     4 spring.datasource.driver-class-name=com.mysql.jdbc.Driver
     5 spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8
     6 spring.datasource.username=root
     7 spring.datasource.password=zhangxf123
     8 
     9 
    10 spring.jpa.show-sql = true
    11   
    12 spring.datasource.druid.initialSize=5
    13 spring.datasource.druid.minIdle=5
    14 spring.datasource.druid.maxActive=20
    15 spring.datasource.druid.maxWait=60000
    16  
    17 spring.datasource.druid.timeBetweenEvictionRunsMillis=60000
    18   
    19 spring.datasource.druid.minEvictableIdleTimeMillis=300000
    20 
    21 spring.datasource.druid.testWhileIdle=true
    22 spring.datasource.druid.testOnBorrow=true
    23 spring.datasource.druid.testOnReturn=false
    24   
    25 spring.datasource.druid.poolPreparedStatements=true
    26 spring.datasource.druid.maxPoolPreparedStatementPerConnectionSize=20
    27 
    28 spring.datasource.druid.filters=stat,wall,log4j
    29 
    30 spring.datasource.druid.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <configuration debug="false">
     3 <!--定义日志文件的存储地址 勿在 LogBack 的配置中使用相对路径-->
     4 <property name="LOG_HOME" value="/home/zhangxiongfeng/logs" />
     5 <property name="LOG_NAME" value="QSurvey" />
     6   <!-- 控制台输出 -->
     7 <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
     8   <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
     9   <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符-->
    10   <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
    11   </encoder>
    12 </appender>
    13   <!-- 按照每天生成日志文件 -->
    14 <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    15   <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
    16   <!--日志文件输出的文件名-->
    17   <FileNamePattern>${LOG_HOME}/${LOG_NAME}.log.%d{yyyy-MM-dd}.log</FileNamePattern>
    18   <!--日志文件保留天数-->  
    19   <MaxHistory>30</MaxHistory>
    20   </rollingPolicy>
    21   <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
    22   <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符-->
    23   <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
    24   </encoder>
    25   <!--日志文件最大的大小-->
    26   <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
    27   <MaxFileSize>10MB</MaxFileSize>
    28   </triggeringPolicy>
    29 </appender>
    30 
    31 <!-- 日志输出级别 -->
    32   <root level="INFO">
    33       <appender-ref ref="STDOUT" />
    34     <appender-ref ref="FILE" />
    35   </root>
    36 </configuration>
    logback.xml
     1 package com.newtouch.mybatise;
     2 
     3 import org.springframework.boot.SpringApplication;
     4 import org.springframework.boot.autoconfigure.SpringBootApplication;
     5 import org.springframework.transaction.annotation.EnableTransactionManagement;
     6 
     7 @SpringBootApplication
     8 @EnableTransactionManagement
     9 public class MybatiseApplication {
    10 
    11     public static void main(String[] args) {
    12         SpringApplication.run(MybatiseApplication.class, args);
    13     }
    14 }
    MybatiseApplication.java
    package com.newtouch.mybatise.controller;
    
    import java.util.HashMap;
    import java.util.Map;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import com.newtouch.mybatise.dao.bean.Student;
    import com.newtouch.mybatise.service.IStudentService;
    
    @RestController
    @RequestMapping("/student")
    public class StudentController {
    
        
        @Autowired
        private IStudentService iStudentService;
        
        @RequestMapping("/add")
        public Map<String, Object> addStudent(){
            Student student = new Student();
            student.setName("张雄峰");
            student.setAge(34);
            iStudentService.addStudent(student);
            Map<String, Object> resultMap = new HashMap<String, Object>();
            resultMap.put("chengong", 1);
            return resultMap;
        }
    }
     1 package com.newtouch.mybatise.service.impl;
     2 
     3 import org.springframework.beans.factory.annotation.Autowired;
     4 import org.springframework.stereotype.Service;
     5 
     6 import com.newtouch.mybatise.dao.IStudentDao;
     7 import com.newtouch.mybatise.dao.bean.Student;
     8 import com.newtouch.mybatise.service.IStudentService;
     9 
    10 @Service
    11 public class StudentServiceImpl implements IStudentService{
    12 
    13     @Autowired
    14     private IStudentDao iStudentDao;
    15     
    16     public void addStudent(Student student){
    17         iStudentDao.addStudent(student);
    18     }
    19     
    20 }
    StudentServiceImpl.java
     1 package com.newtouch.mybatise.dao.impl;
     2 
     3 import org.springframework.beans.factory.annotation.Autowired;
     4 import org.springframework.stereotype.Repository;
     5 
     6 import com.newtouch.mybatise.dao.IStudentDao;
     7 import com.newtouch.mybatise.dao.bean.Student;
     8 import com.newtouch.mybatise.dao.mapper.IStudentMapper;
     9 
    10 @Repository
    11 public class StudentDaoImpl implements IStudentDao {
    12     
    13     @Autowired
    14     private IStudentMapper iStudentMapper;    
    15 
    16     
    17     public void addStudent(Student student){
    18         iStudentMapper.insert(student);
    19     }
    20     
    21 }
    StudentDaoImpl.java
     1 package com.newtouch.mybatise.dao.bean;
     2 
     3 /**
     4  * 
     5  * @author zhangxiongfeng
     6  *
     7  */
     8 public class Student {
     9 
    10     private int id;
    11     private String name;
    12     private int age;
    13     
    14     public int getId() {
    15         return id;
    16     }
    17     public void setId(int id) {
    18         this.id = id;
    19     }
    20     public String getName() {
    21         return name;
    22     }
    23     public void setName(String name) {
    24         this.name = name;
    25     }
    26     public int getAge() {
    27         return age;
    28     }
    29     public void setAge(int age) {
    30         this.age = age;
    31     }
    32 
    33 }
    Student.javas

    package com.newtouch.mybatise.dao.mapper;

    import org.apache.ibatis.annotations.Insert;
    import org.apache.ibatis.annotations.Mapper;
    import org.apache.ibatis.annotations.Options;

    import com.newtouch.mybatise.dao.bean.Student;

    @Mapper
    public interface IStudentMapper {

    @Insert("insert into student(name,age) values(#{name},#{age})")
    @Options(useGeneratedKeys = true, keyColumn = "id", keyProperty = "id")
    void insert(Student Student);
    }

    springboot mybatise 注解实现最主要的类

  • 相关阅读:
    表格标签
    常用标签
    标签笔记
    基础标签与格式
    态度!
    如何修改数据 练习
    增删查练习
    登陆注册练习
    PHP 数据访问
    PHP 基础知识测试题 答案分析
  • 原文地址:https://www.cnblogs.com/zxf330301/p/9083908.html
Copyright © 2011-2022 走看看