### 1. 在SpringBoot框架集成MyBatis,使用MyBatis框架操作数据。
使用步骤:
-
添加mybatis起步依赖:完成mybatis对象自动配置,将对象放在容器中
-
pom.xml中指定把src/main/java目录中的xml文件包含到classpath中
-
创建实体类User
-
创建mapper接口UserMapper,创建操作用户的方法
-
创建mapper接口对应的Mapper文件,就是写sql语句的xml文件
-
创建Service层对象,创建UserService接口和它的实现类。去调用mapper接口对象的方法,完成数据库的操作。
-
创建Controller层对象,调用Service层。
-
写application.properties文件,配置数据库的连接信息。
@Mapper:放在mapper接口的上面,每个接口都要使用这个注解
@Mapper
@Repository
public interface UserMapper {
List<User> findAll();
}
UserMapper.xml:
<mapper namespace="com.cn.wanxi.mapper.UserMapper">
<select id="findAll" resultType="com.cn.wanxi.model.User">
select * from user
</select>
</mapper>
application.properties配置文件:
server.port=8086
#连接数据库
#连接数据库的驱动
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/medicaluseUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=root
service的实现类:UserServiceImpl.java:
@Service("UserService")
public class UserServiceImpl implements UserService {
@Resource
private UserMapper userMapper;
@Override
public ResultModel findAll() {
List<User> list = userMapper.findAll();
return ResultModel.getResultModel(list);
}
}
UserController.java:
@RestController
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/findAll")
public ResultModel findAll(){
ResultModel resultModel = userService.findAll();
return resultModel;
}
}
### 2. springboot JPA
在pom.xml中导入依赖: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency>
Customer实体类:
@Entity
@Table(name = "customer")
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Integer id;
@Column(name = "name")
private String name;
@Column(name = "phone")
private String phone;
@Column(name = "level")
private String level;
@Column(name = "address")
private String address;
// 一个客户包含多个联系人
// 配置Customer为1对多,关系由LinkMan方维护(多方维护)
// mappedBy 里的名字就是LinkMan里Customer的变量名customer
//cascade=cascade.Remove :级联删除
@OneToMany(mappedBy = "customer",fetch = FetchType.EAGER,cascade = {CascadeType.REMOVE})
Set<Linkman> linkmanSet;
Linkman实体类:
@Entity
@Table(name = "link_man")
public class Linkman {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Integer id;
@Column(name = "name")
private String name;
@Column(name = "phone")
private String phone;
@Column(name = "position")
private String position;
// 多对一,targeEntity表示1的一方是谁
@ManyToOne(targetEntity = Customer.class)
// JoinColumn表示主外键。name:外键,referencedColumnName:外键对应的主键
@JoinColumn(name = "customer_id",referencedColumnName = "id")
private Customer customer;
客户测试类CustomerRepositoryTest.java:
@SpringBootTest
class CustomerRepositoryTest {
@Autowired
CustomerRepository customerRepository;
@Test
public void add_customer_success(){
Customer customer = new Customer();
customer.setName("wangwei");
customer.setAddress("chengdu");
customer.setLevel("middle");
customer.setPhone("19839278456");
customerRepository.save(customer);
System.out.println(customer);
}
在执行该测试类时:
报错原因:这是因为在添加的客户信息中出现中文。而问题可能有3个:
1.首先查看数据库的字符集是否是utf8
2.查看表中字段的字符集是否是utf8
3.查看项目配置文件连接数据库的URL