spring+mybatise注解实现

时间:2023-12-16 21:26:32

spring+mybatise注解实现

 spring.jpa.database=MYSQL

 spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=zhangxf123 spring.jpa.show-sql = true spring.datasource.druid.initialSize=5
spring.datasource.druid.minIdle=5
spring.datasource.druid.maxActive=20
spring.datasource.druid.maxWait=60000 spring.datasource.druid.timeBetweenEvictionRunsMillis=60000 spring.datasource.druid.minEvictableIdleTimeMillis=300000 spring.datasource.druid.testWhileIdle=true
spring.datasource.druid.testOnBorrow=true
spring.datasource.druid.testOnReturn=false spring.datasource.druid.poolPreparedStatements=true
spring.datasource.druid.maxPoolPreparedStatementPerConnectionSize=20 spring.datasource.druid.filters=stat,wall,log4j spring.datasource.druid.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
 <?xml version="1.0" encoding="UTF-8"?>
<configuration debug="false">
<!--定义日志文件的存储地址 勿在 LogBack 的配置中使用相对路径-->
<property name="LOG_HOME" value="/home/zhangxiongfeng/logs" />
<property name="LOG_NAME" value="QSurvey" />
  <!-- 控制台输出 -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
  <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
  <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符-->
  <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
  </encoder>
</appender>
  <!-- 按照每天生成日志文件 -->
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
  <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
  <!--日志文件输出的文件名-->
  <FileNamePattern>${LOG_HOME}/${LOG_NAME}.log.%d{yyyy-MM-dd}.log</FileNamePattern>
  <!--日志文件保留天数-->  
  <MaxHistory>30</MaxHistory>
  </rollingPolicy>
  <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
  <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符-->
  <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
  </encoder>
  <!--日志文件最大的大小-->
  <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
  <MaxFileSize>10MB</MaxFileSize>
  </triggeringPolicy>
</appender> <!-- 日志输出级别 -->
  <root level="INFO">
   <appender-ref ref="STDOUT" />
<appender-ref ref="FILE" />
  </root>
</configuration>

logback.xml

 package com.newtouch.mybatise;

 import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.transaction.annotation.EnableTransactionManagement; @SpringBootApplication
@EnableTransactionManagement
public class MybatiseApplication { public static void main(String[] args) {
SpringApplication.run(MybatiseApplication.class, args);
}
}

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;
}
}
 package com.newtouch.mybatise.service.impl;

 import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import com.newtouch.mybatise.dao.IStudentDao;
import com.newtouch.mybatise.dao.bean.Student;
import com.newtouch.mybatise.service.IStudentService; @Service
public class StudentServiceImpl implements IStudentService{ @Autowired
private IStudentDao iStudentDao; public void addStudent(Student student){
iStudentDao.addStudent(student);
} }

StudentServiceImpl.java

 package com.newtouch.mybatise.dao.impl;

 import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository; import com.newtouch.mybatise.dao.IStudentDao;
import com.newtouch.mybatise.dao.bean.Student;
import com.newtouch.mybatise.dao.mapper.IStudentMapper; @Repository
public class StudentDaoImpl implements IStudentDao { @Autowired
private IStudentMapper iStudentMapper; public void addStudent(Student student){
iStudentMapper.insert(student);
} }

StudentDaoImpl.java

 package com.newtouch.mybatise.dao.bean;

 /**
*
* @author zhangxiongfeng
*
*/
public class Student { private int id;
private String name;
private int age; public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
} }

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 注解实现最主要的类