Commit 17cf909e authored by 杨伊博's avatar 杨伊博

improve and perfect mybatis

parent 8e56c3b8
package com.us.example;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableScheduling;
import static org.springframework.boot.SpringApplication.*;
/**
......@@ -11,7 +11,7 @@ import static org.springframework.boot.SpringApplication.*;
@ComponentScan(basePackages ="com.us.example")
@SpringBootApplication
@EnableCaching
@EnableScheduling
public class Application {
public static void main(String[] args) {
ConfigurableApplicationContext run = run(Application.class, args);
......
package com.us.example.config;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
//@Retention: 定义注解的保留策略,注释类型的注释要保留
@Retention(RetentionPolicy.RUNTIME)
//@Target说明了Annotation所修饰的对象范围:Annotation可被用于 packages、types(类、接口、枚举、Annotation类型)、类型成员(方法、构造方法、成员变量、枚举值)、方法参数和本地变量(如循环变量、catch参数)。在Annotation类型的声明中使用了target可更加明晰其修饰的目标。
//作用:用于描述注解的使用范围(即:被描述的注解可以用在什么地方)
//取值(ElementType)有:
//CONSTRUCTOR:用于描述构造器
//2.FIELD:用于描述域
//3.LOCAL_VARIABLE:用于描述局部变量
//4.METHOD:用于描述方法
//5.PACKAGE:用于描述包
//6.PARAMETER:用于描述参数
//7.TYPE:用于描述类、接口(包括注解类型) 或enum声明
@Target(ElementType.TYPE)
public @interface MyBatisRepository {
}
......@@ -10,7 +10,7 @@ public class MyBatisScannerConfig {
public MapperScannerConfigurer MapperScannerConfigurer() {
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
mapperScannerConfigurer.setBasePackage("com.us.example.dao");
mapperScannerConfigurer.setAnnotationClass(MyBatisRepository.class);
// mapperScannerConfigurer.setAnnotationClass(MyBatisRepository.class);
mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
return mapperScannerConfigurer;
}
......
......@@ -4,9 +4,7 @@ import java.util.List;
import java.util.Map;
import com.us.example.bean.User;
import com.us.example.config.MyBatisRepository;
@MyBatisRepository
public interface UserDao {
public List<User> getList(Map<String,Object> map);
}
......@@ -4,6 +4,7 @@ package com.us.example.serviceImpl;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import com.us.example.dao.UserDao;
......@@ -28,7 +29,14 @@ public class UserServiceImpl implements UserService {
* @return
*/
public Object getList(Map<String, Object> map) {
printName();
return userDao.getList(map);
}
//每一秒调用一次 -- 用于测试
@Scheduled(cron = "0/1 * * * * ?")
public void printName() {
System.out.println("my name is yang ");
}
}
\ No newline at end of file
......@@ -3,7 +3,7 @@ server.port=8099
ms.db.driverClassName=com.mysql.jdbc.Driver
ms.db.url=jdbc:mysql://localhost:3306/msm?prepStmtCacheSize=517&cachePrepStmts=true&autoReconnect=true&characterEncoding=utf-8&allowMultiQueries=true
ms.db.url=jdbc:mysql://localhost:3306/msm?useSSL=false&useUnicode=true&characterEncoding=UTF-8
ms.db.username=root
ms.db.password=admin
ms.db.maxActive=500
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment