一、java集成mybatis
1.application.properties增加spring配置数据库链接地址
2.pom.xml引入mybatis-spring-boot-starter和mysql-connector-java
具体操作:
1.application.properties中加入如下代码
#1.首先配置数据库的连接(集成mybatis第一步,配置数据库)
spring.datasource.url=jdbc:mysql://localhost:3306/toutiao?useUnicode=true&characterEncoding=utf8&useSSL=false
#用户名和密码
spring.datasource.username=root
spring.datasource.password=123456
#添加配置文件,下载自官网
mybatis.config-location=classpath:mybatis-config.xml
2.pom.xml中添加如下依赖
<!--集成mybatis第二步,引入依赖-->
<!--引入mysql连接器-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--引入mybatis和spring boot的启动器-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
二、在test->java->com.nowcoder->resources中添加sql文件“init-schema.sql”
##都是创建sql表的语句,创建两个表
CREATE TABLE `user` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(64) NOT NULL DEFAULT '',
`password` varchar(128) NOT NULL DEFAULT '',
`salt` varchar(32) NOT NULL DEFAULT '',
`head_url` varchar(256) NOT NULL DEFAULT '',
PRIMARY KEY (`id`),
UNIQUE KEY `name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `news`;
CREATE TABLE `news` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(128) NOT NULL DEFAULT '',
`link` varchar(256) NOT NULL DEFAULT '',
`image` varchar(256) NOT NULL DEFAULT '',
`like_count` int NOT NULL,
`comment_count` int NOT NULL,
`created_date` datetime NOT NULL,
`user_id` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
三、下载“mybatis-config.xml”文件,放入mian->java->com.nowcoder->resources
四、在main->java->com.nowcoder->model中创建user实体(就是定义各种键值,get和set方法)
package com.nowcoder.model;
//定义数据库实体User
public class User {
private int id;
private String name;
private String password;
private String salt;
private String headUrl;
public User(){
}
public User(String name){
this.name = name;
this.password = "";
this.salt = "";
this.headUrl = "";
}
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 String getPassword() { return password; }
public void setPassword(String password) { this.password = password; }
public String getSalt() { return salt; }
public void setSalt(String salt) { this.salt = salt; }
public String getHeadUrl() { return headUrl; }
public void setHeadUrl(String headUrl) { this.headUrl = headUrl; }
}
五、在DAO层创建user接口,路径:mian->java->com.nowcoder->dao
import org.apache.ibatis.annotations.*;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Repository;
//DAO层,是数据库的入口
@Mapper//该标注表示为数据库入口
public interface UserDAO {
String TABLE_NAME = "user";
String INSERT_FILEDS = " name,password, salt, head_url ";
String SELECT_FILEDS = " id, name, password, salt, head_url ";
//主要包含增查更删四个方法
//insert方法——插入User,感觉可以用void,输入参数为User
@Insert({"insert into ", TABLE_NAME, "(",INSERT_FILEDS, ") values (#{name},#{password},#{salt},#{headUrl})"})
int addUser(User user);
//select方法——选择用户,输入id,返回User
@Select({"select ", SELECT_FILEDS, " from ", TABLE_NAME, " where id=#{id}"})
User selectById(int id);
//update方法——更新,输入User,返回空
@Update({"update ", TABLE_NAME, " set passwod=#{password} where id=#{id} "})
void updatePassword(User user);
//delete方法——删除,输入id,返回空
@Delete({"delete from ", TABLE_NAME, " where id=#{id}"})
void deleteById(int id);
}
六、写一个测试类(测试是否能创建数据库),路径:test->java->com.nowcoder
@SpringApplicationConfiguration(classes = ToutiaoApplication.class)
//导入数据库文件,即跑之前创建的数据库
@Sql("/init-schema.sql")
public class InitDatabaseTests{
@Autowired
//这里除了问题,总是提示“could not autowire.No beans of 'UserDAO' type found”
UserDAO userDAO;
@Test
public void initData(){
Random random = new Random();
//创建11个User
for (int i = 0; i < 11; ++i){
//调用model里面的方法创建user
User user = new User();
user.setHeadUrl(String.format("http://images.nowcoder.com/head/%dt.png",random.nextInt(1000)));
user.setName(String.format("USER%d",i));
user.setPassword("");
user.setSalt("");
//调用dao层接口,插入数据库
userDAO.addUser(user);
}
}
}
按理说,这个执行测试类后,就会在toutiao的数据库中生成11个用户数据,但是测试类@Autowired处出了问题,而且从网上找了好久没找到能解决的答案,所以先在这留个flag,后面继续学习下,如果有大佬知道的话,希望能给个提示。