SpringBoot和Mybatis的整合
全注解版
SpringBoot配置文件
这里使用yml格式的配置文件,将application.properties改名为application.yml。
#配置数据源
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/dianping?useUnicode=true&characterEncoding=utf8
username: root
password: 123
driver-class-name: com.mysql.jdbc.Driver
SpringBoot会自动加载application.yml相关配置,数据源就会自动注入到sqlSessionFactory中,sqlSessionFactory会自动注入到Mapper中。
实体类
public class Happiness {
private Long id;
private String city;
private Integer num;
//getters、setters、toString
}
映射类
@Mapper
public interface HappinessDao {
@Select("SELECT * FROM happiness WHERE city = #{city}")
Happiness findHappinessByCity(@Param("city") String city);
@Insert("INSERT INTO happiness(city, num) VALUES(#{city}, #{num})")
int insertHappiness(@Param("city") String city, @Param("num") Integer num);
}
Service类
事务管理只需要在方法上加个注解:@Transactional
@Service
public class HappinessService {
@Autowired
private HappinessDao happinessDao;
public Happiness selectService(String city){
return happinessDao.findHappinessByCity(city);
}
@Transactional
public void insertService(){
happinessDao.insertHappiness("西安", 9421);
int a = 1 / 0; //模拟故障
happinessDao.insertHappiness("长安", 1294);
}
}
Controller类
@RestController
@RequestMapping("/demo")
public class HappinessController {
@Autowired
private HappinessService happinessService;
@RequestMapping("/query")
public Happiness testQuery(){
return happinessService.selectService("北京");
}
@RequestMapping("/insert")
public Happiness testInsert(){
happinessService.insertService();
return happinessService.selectService("西安");
}
}
测试
http://localhost:8080/demo/query
http://localhost:8080/demo/insert
注解xml合并版
项目结构
SpringBoot配置文件
#配置数据源
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/dianping
username: root
password: 123
driver-class-name: com.mysql.jdbc.Driver
#指定mybatis映射文件的地址
mybatis:
mapper-locations: classpath:mapper/*.xml
映射类
@Mapper
public interface HappinessDao {
Happiness findHappinessByCity(String city);
int insertHappiness(HashMap<String, Object> map);
}
映射文件
<mapper namespace="com.example.demo.dao.HappinessDao">
<select id="findHappinessByCity" parameterType="String" resultType="com.example.demo.domain.Happiness">
SELECT * FROM happiness WHERE city = #{city}
</select>
<insert id="insertHappiness" parameterType="HashMap" useGeneratedKeys="true" keyProperty="id">
INSERT INTO happiness(city, num) VALUES(#{city}, #{num})
</insert>
</mapper>
Service类
事务管理只需要在方法上加个注解:@Transactional
@Service
public class HappinessService {
@Autowired
private HappinessDao happinessDao;
public Happiness selectService(String city){
return happinessDao.findHappinessByCity(city);
}
@Transactional
public void insertService(){
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("city", "西安");
map.put("num", 9421);
happinessDao.insertHappiness(map);
int a = 1 / 0; //模拟故障
happinessDao.insertHappiness(map);
}
}
Controller类
@RestController
@RequestMapping("/demo")
public class HappinessController {
@Autowired
private HappinessService happinessService;
@RequestMapping("/query")
public Happiness testQuery(){
return happinessService.selectService("北京");
}
@RequestMapping("/insert")
public Happiness testInsert(){
happinessService.insertService();
return happinessService.selectService("西安");
}
}
测试
http://localhost:8080/demo/query
http://localhost:8080/demo/insert
--------------------- 作者:椰子Tyshawn 来源:CSDN 原文:https://blog.csdn.net/litianxiang_kaola/article/details/79481422?utm_source=copy 版权声明:本文为博主原创文章,转载请附上博文链接!