本文小編為大家詳細(xì)介紹“怎么在springboot中集成mybatis xml方式”,內(nèi)容詳細(xì),步驟清晰,細(xì)節(jié)處理妥當(dāng),希望這篇“怎么在springboot中集成mybatis xml方式”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學(xué)習(xí)新知識(shí)吧。
10年積累的網(wǎng)站設(shè)計(jì)、成都網(wǎng)站設(shè)計(jì)經(jīng)驗(yàn),可以快速應(yīng)對客戶對網(wǎng)站的新想法和需求。提供各種問題對應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先網(wǎng)站制作后付款的網(wǎng)站建設(shè)流程,更有平橋免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
springboot集成mybatis
1,添加pom引用
org.mybatis.spring.boot mybatis-spring-boot-starter 1.1.1 MySQL mysql-connector-java
2 application.properties
mybatis.config-locations=classpath:mybatis/mybatis-config.xml mybatis.mapper-locations=classpath:mybatis/mapper/*.xml mybatis.type-aliases-package=com.kerry.model spring.datasource.driverClassName = com.mysql.jdbc.Driver spring.datasource.url = jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8 spring.datasource.username = root spring.datasource.password = 123456
3 在resource目錄下創(chuàng)建mybatis目錄并創(chuàng)建mybatis-config.xml文件
mybatis目錄下創(chuàng)建mapper目錄存放mapper類接口文件
package com.kerry.mapper; import java.util.List; import com.kerry.model.User; public interface UserMapper { ListgetAll(); User getOne(Integer id); void insert(User user); void update(User user); void delete(Integer id); }
model類文件
package com.kerry.mapper; import java.util.List; import com.kerry.model.User; public interface UserMapper { ListgetAll(); User getOne(Integer id); void insert(User user); void update(User user); void delete(Integer id); }
userMapper.xml
id, name, age, address INSERT INTO user (id,name,age,address) VALUES (#{id},#{name}, #{age}, #{address}) UPDATE user SET name = #{name}, age = #{age}, address = #{address} WHERE id = #{id}DELETE FROM user WHERE id =#{id}
controller:
package com.kerry.web; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.kerry.model.User; import com.kerry.mapper.UserMapper; @RestController public class UserController { @Autowired private UserMapper userMapper; @RequestMapping("/getUsers") public ListgetUsers() { List users=userMapper.getAll(); return users; } @RequestMapping("/getUser") public User getUser(Integer id) { User user=userMapper.getOne(id); return user; } @RequestMapping("/add") public void save(User user) { userMapper.insert(user); } @RequestMapping(value="update") public void update(User user) { userMapper.update(user); } @RequestMapping(value="/delete/{id}") public void delete(@PathVariable("id") Integer id) { userMapper.delete(id); } }
最后在啟動(dòng)類上加上 掃描maper interface注解
@SpringBootApplication @MapperScan("com.kerry.mapper") public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
或者在每個(gè)XXMapper類上加上@mapper注解也行 二選一即可
比如
package com.kerry.mapper; import java.util.List; import org.apache.ibatis.annotations.Mapper; import com.kerry.model.User; @Mapper public interface UserMapper { ListgetAll(); User getOne(Integer id); void insert(User user); void update(User user); void delete(Integer id); }
個(gè)人建議使用直接在啟動(dòng)類上application上@MapperScan方便,一次搞定,不必每寫一個(gè)mapper類都加上@mapper注解
附上項(xiàng)目結(jié)構(gòu)目錄 選中的類以及文件
讀到這里,這篇“怎么在springboot中集成mybatis xml方式”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識(shí)點(diǎn)還需要大家自己動(dòng)手實(shí)踐使用過才能領(lǐng)會(huì),如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。