springboot+mybatis结合使用与普通的ssm配置差别不大,但是少了很多的配置,如spring.xml web.xml, 给程序员减轻了很多负担
首先创建带有mybatis框架的项目
*.xml配置与ssm无异
配置application.properties
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/m1
spring.datasource.username=root
spring.datasource.password=mlh123456
mybatis.type-aliases-package=com.test.entity
mybatis.mapper-locations=classpath:mapper/*.xml
entity层
package com.test.entity; /** * Created by MY on 2017/8/15. */ public class Girls { private Integer id; private String cup_size; private Integer age; public Girls(Integer id, String cup_Size, Integer age) { this.id = id; this.cup_size = cup_size; this.age = age; } public String getCup_size() { return cup_size; } public void setCup_size(String cup_Size) { this.cup_size = cup_Size; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Girls() { } }
dao层 **类名注解** @Mapper该注解只表示与*.xml的映射,不能表示将该对象交给spring管理
package com.test.dao; import com.test.entity.Girls; import org.apache.ibatis.annotations.Mapper; import org.springframework.stereotype.Repository; import java.util.List; /** * Created by MY on 2017/8/15. */ @Mapper @Repository public interface GirlsDao { List<Girls> findAllGirls(); }
controller层
package com.test.contoller; import com.test.dao.GirlsDao; import com.test.entity.Girls; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import java.util.List; /** * Created by MY on 2017/8/15. */ @Controller public class FirstController { @Autowired private GirlsDao gd; @RequestMapping("first") public String first(){ return "abc"; } @ResponseBody @RequestMapping("find.do") public List<Girls> findAllGirls(){ return gd.findAllGirls(); } }