发表于: 2018-03-28 20:45:29
1 598
今日完成
1.做了一道多线程的面试题
要求:子线程执行10次,主线程再运行5次,这样交替进行三遍
public class ThreadDemo2 {
public static void main(String[] args) {
final Busssiness b = new Busssiness();
Thread a = new Thread(new Runnable() {
@Override
public void run() {
//主线程打印,3次
for (int i = 0; i < 3; i++) {
b.main();
}
}
});
Thread a1 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 3; i++) {
//子线程打印3次
b.sub();
}
}
});
a.start();
a1.start();
}
}
class Busssiness {
Boolean flag = true;
public synchronized void main() {
while (flag) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName() + "main...");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
flag = true;
notify();
}
public synchronized void sub() {
while (!flag) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName() + "sub...");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
flag = false;
notify();
}
}
2.spring boot 中mybatis的使用方式。-----基于xml的方式
(1)引入依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
(2)配置数据源和连接池
spring.datasource.url = jdbc:mysql://localhost:3306/task4?useUnicode=true&characterEncoding=utf-8
spring.datasource.username = root
spring.datasource.password = root
spring.datasource.driver-class-name = com.mysql.jdbc.Driver
如果需要使用第三方的连接池,需要在启动类里面注入
(2)配置bean类和dao接口和xml配置文件,这里的使用和spring的方式一样的。不一样的地方就是,需要将xml的配置文件放在resources里面。然后在主配置文件中指定
#指定bean所在包
mybatis.type-aliases-package=com.lujing.model
#指定映射文件
mybatis.mapperLocations=classpath:mapper/*.xml
接口的扫描有两种方式
第一是直接在接口上使用@mapper注解
第二种,在启动类上制订扫描的包
(3)写一个测试类进行测试
(4)写一个controller
@RestController
public class LearnController {
@Autowired
LearnMapper learnMapper;
@RequestMapping("/get")
public Learn get(){
return learnMapper.selectByPrimaryKey(1);
}
}
明日计划
1.拆解task
2.部署接口
遇到问题
暂无
收获
springbootmybatis
评论