您好,登錄后才能下訂單哦!
添加mybatis pom.xml依賴
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.0</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency>
添加application.properties mysql配置
spring.datasource.url=jdbc:mysql://localhost:3306/test?useSSL=false&useUnicode=true&characterEncoding=utf-8 spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.jdbc.Driver
mysql test庫中創(chuàng)建account表,springboot添加account model類
@Component public class Account { private int id ; private String name ; private double money; 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 double getMoney() { return money; } public void setMoney(double money) { this.money = money; } }
創(chuàng)建DAO層接口
import com.example.demo.domain.Account; import org.apache.ibatis.annotations.*; import org.mapstruct.Mapper; import org.springframework.stereotype.Component; import org.springframework.stereotype.Repository; import java.util.List; /** * @Author 馮戰(zhàn)魁 * @Date 2018/1/23 上午9:59 */ @Mapper @Component(value = "accountMapper") public interface AccountMapper { @Insert("insert into account(name, money) values(#{name}, #{money})") int add(@Param("name") String name, @Param("money") double money); @Update("update account set name = #{name}, money = #{money} where id = #{id}") int update(@Param("name") String name, @Param("money") double money, @Param("id") int id); @Delete("delete from account where id = #{id}") int delete(int id); @Select("select id, name as name, money as money from account where id = #{id}") Account findAccount(@Param("id") int id); @Select("select id, name as name, money as money from account") List<Account> findAccountList(); }
入口文件加入@MapperScan掃描接口的路徑
@SpringBootApplication @MapperScan("com.example.demo.repository") public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
添加操作接口
import com.example.demo.repository.AccountMapper; import com.example.demo.domain.Account; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.List; /** * @Author 馮戰(zhàn)魁 * @Date 2018/1/23 上午10:12 */ @RestController @RequestMapping("/account") public class AccountController { @Autowired AccountMapper accountMapper; @RequestMapping("list") public List<Account> getAccounts() { return accountMapper.findAccountList(); } @RequestMapping(value = "/{id}", method = RequestMethod.GET) public Account getAccountById(@PathVariable("id") int id) { return accountMapper.findAccount(id); } @RequestMapping(value = "/{id}", method = RequestMethod.PUT) public String updateAccount(@PathVariable("id") int id, @RequestParam(value = "name", required = true) String name, @RequestParam(value = "money", required = true) double money) { int t= accountMapper.update(name,money,id); if(t==1) { return "success"; }else { return "fail"; } } @RequestMapping(value = "/{id}", method = RequestMethod.DELETE) public String delete(@PathVariable(value = "id")int id) { int t= accountMapper.delete(id); if(t==1) { return "success"; }else { return "fail"; } } @RequestMapping(value = "", method = RequestMethod.POST) public String postAccount(@RequestParam(value = "name") String name, @RequestParam(value = "money") double money) { int t= accountMapper.add(name,money); if(t==1) { return "success"; }else { return "fail"; } } }
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。