Spring Boot 数据访问
application.yami配置
引入依赖包druid-spring-boot-starter
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>
druid-spring-boot-starter
</artifactId>
<version>1.2.8</version>
</dependency>
<!--导入驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>
mysql-connector-java
</artifactId>
<version>8.0.26</version>
</dependency>
①配置文件配置MySQL接口和数据库驱动Driver
# 配置数据库连接
spring:
datasource:
url: jdbc:mysql://localhost:3306/test
username: gcs
password: 273869
driver-class-name: com.mysql.cj.jdbc.Driver
②利用JdbcTemplate调用sql语句
#在方法里放入执行语句
jdbcTemplate.execute("select * from user");
mybatis.xml+Mapper.xml
引入依赖包mybatis-spring-boot-starter
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>
mybatis-spring-boot-starter
</artifactId>
<version>2.2.0</version>
</dependency>
<!--导入驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>
mysql-connector-java
</artifactId>
<version>8.0.26</version>
</dependency>
①创建mybatis.xml配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property
name="driver"
value="com.mysql.jdbc.Driver"/>
<property
name="url"
value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=utf8"/>
<property name="username" value="gcs"/>
<property name="password" value="273869"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/example/spring_mybatis/mapper/userMapper.xml"/>
</configuration>
②创建接口Mapper
@Mapper
public interface AccountMapper{
#构建方法
public Account getAccount(String name);
}
③创建mapper配置文件Mapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace= "com.example.managesystemdemo.mapper.AccountMapper">
<!--id为Mapper接口里面的方法名,namespace为Mapper接口的地址-->
<select id="getAccount" resultType="com.example.managesystemdemo.bean.Account">
select * from user where username=#{name}
</select>
</mapper>
④为Mapper接口创建服务接口和实现类
@Data
@Component
@ToString
@AllArgsConstructor
@NoArgsConstructor
public class Account {
private String username;
private String pwd;
}
public interface AccountService {
public Account getUser(String name);
}
@Service
public class AccountServiceImpl
implements AccountService {
@Autowired
AccountMapper accountMapper;
public Account getUser(String name) {
//调用mapper接口方法
return accountMapper.getAccount(name);
}
}
⑤利用服务接口实现数据访问
@Autowired
AccountService accountService;
@ResponseBody
@GetMapping("/select")
public Account select(@RequestParam String name
{
return accountService.getUser(name);
}
Mapper接口+注释
引入依赖包导入mybatis-plus包,自带jdbc驱动,所以不需要导入驱动包mysql-connector-java
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>
mybatis-plus-boot-starter
</artifactId>
<version>3.4.1</version>
</dependency>
①创建Mapper接口
public interface AccountMapper {
/**
* 有@Insert注释就不需要配置mapper.xml文件了
*/
@Insert("insert into user
values(#{username},#{pwd})")
public void insertAccount(Account account);
}
②创建服务接口和实现类
@Data
@Component
@ToString
@AllArgsConstructor
@NoArgsConstructor
public class Account {
private String username;
private String pwd;
}
public interface AccountService {
public void insertAccount(Account account);
}
@Service
public class AccountServiceImpl
implements AccountService {
@Autowired
AccountMapper accountMapper;
@Override
public void insertAccount(Account account)
{
//调用mapper接口方法
accountMapper.insertAccount(account);
}
}
③利用服务类访问数据
City city = new City("上饶","江西","中国");
@Autowired
AccountService accountService;
@ResponseBody
@GetMapping("/insert")
public String insert()
{
cityService.insert(city);
return "{city.toString()}插入成功!!!";
}
BaseMapper+@TableName()
引入依赖mybatis-plus
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>
mybatis-plus-boot-starter
</artifactId>
<version>3.4.1</version>
</dependency>
①创建数据表映射的类
@Data
@Component
@ToString
@NoArgsConstructor
@AllArgsConstructor
//默认表名为类名小写
@TableName("user_info")
public class User {
//所有属性都应该在数据表中,属性名和表中字段名相同
//除了有TableField(exist = false)注释的
//默认主键为自增,如果主键不是自增得说明,不然会报错
@TableId(type = IdType.AUTO)
private String username;
@TableField(exist = false)
//表中没有该属性对应的字段名
private String password;
private String fullname;
private String email;
private String address;
private String city;
private String sex;
}
②创建Mapper接口
/**
* 继承BaseMapper就有操作数据库的方法,不需要写Mapper.xml
*/
@Mapper
public interface UserMapper extends BaseMapper<User> {
}
③创建服务接口和实现类
/**
* 继承IService<User>类,里面有所有的MySQL执行方法
*/
public interface UserService extends IService<User> {
}
/**
* 继承ServiceImpl<UserMapper,User>类就不需要重写
* IService接口里面的所有方法
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
}
④利用服务接口访问数据
@Autowired
UserService userService;
@ResponseBody
@GetMapping("/userInfo")
public List<User> getUserInfo()
{
//BaseMapper<>类自带很多MySQL执行方法
return userService.list();
}