日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Spring Boot:如何快速集成Mybatis和Thymeleaf

前言

有時候做方案,需要模擬一些業(yè)務(wù)上的一些場景來驗證方案的可行性,基本上每次都是到處百度如何集成springboot+mybatis+thymeleaf這些東西的集成平時基本上一年也用不了一次,雖然比較簡單,奈何我真得記不住詳細的每一步,因此每次都是從零開始,我一直在想,把時間浪費在這種重復(fù)的事情是沒有意義的,所以這篇文章記錄一下,以后再也不到處百度來接拼湊了。

從事德陽電信服務(wù)器托管,服務(wù)器租用,云主機,雅安服務(wù)器托管主機域名,CDN,網(wǎng)絡(luò)代維等服務(wù)。

目標

springboot中集在mybatis和thymeleaf,簡單實現(xiàn)一下新增和查詢功能,后續(xù)有需要再往上補。

圖片

環(huán)境配置

jdk版本:1.8

開發(fā)工具:Intellij iDEA 2020.1

springboot:2.3.9.RELEASE

具體步驟

依賴引入

主要引入了springboot、thymeleaf、mybais、mysql、jdbc以及熱部署和lombda相關(guān)的依賴;


    org.springframework.boot
    spring-boot-starter


    org.springframework.boot
    spring-boot-starter-web


    org.springframework.boot
    spring-boot-starter-test
    test




    org.springframework.boot
    spring-boot-starter-thymeleaf


    ognl
    ognl
    3.1.26


    org.projectlombok
    lombok


    org.springframework.boot
    spring-boot-devtools
    true


    org.mybatis.spring.boot
    mybatis-spring-boot-starter
    2.1.4


    org.springframework.boot
    spring-boot-starter-jdbc


    mysql
    mysql-connector-java

配置文件

配置文件這里新增了三處配置,分別是thymeleaf、數(shù)據(jù)庫連接、mybatis;

#thymeleaf配置
spring.thymeleaf.cache=false
spring.thymeleaf.suffix=.html
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.servlet.content-type=text/html
spring.thymeleaf.encoding=utf-8
#數(shù)據(jù)庫連接配置
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3307/happy_home?serverTimeznotallow=Asia/Shanghai 
spring.datasource.username=root
spring.datasource.password=root
#mybatis配置
mybatis.mapper-locatinotallow=classpath:/mapper/*.xml
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

圖片

前端代碼

1、resources/static目錄下,新增靜態(tài)文件index.html;




    
    Title


登陸名:
姓名:
性別:
手機號碼:
身份證號:
地址:
門牌號:

2、resources/templates目錄上,新增home.html文件;




    
    主頁
    


ID:

登陸名:

姓名:

性別:

手機號碼:

身份證號:

地址:

門牌號:

后端代碼

1、PersonController.java

@Controller
@RequestMapping("/person")
public class PersonController {
    @Autowired
    private IPersonService personService;
    
    @PostMapping("/registe")
    public String registe(Person person, Model model) {
        Integer id = this.personService.registe(person);
        model.addAttribute("id", id);
        return "home";
    }


    @GetMapping("/{id}")
    @ResponseBody
    public Person getPerson(@PathVariable("id") Integer id) {
        Person person = this.personService.get(id);
        return person;
    }
}

2、IPersonService.java

public interface IPersonService {
    Integer registe(Person person);
    Person get(Integer id);
}

3、PersonServiceImpl.java

@Service
public class PersonServiceImpl implements IPersonService {
    @Autowired
    private PersonDao personDao;
    @Override
    public Integer registe(Person person) {
         this.personDao.insert(person);
        return person.getId();
    }
    @Override
    public Person get(Integer id) {
        Person persnotallow=personDao.selectById(id);
        return person;
    }
}

4、PersonDao.java

@Mapper
public interface PersonDao {
    Integer insert(Person person);
    Person selectById(Integer id);
}

5、PersonMapper.xml




    
        
        
        
        
        
        
        
        
    
    
        insert into sys_person(user_name, login_no, phone_number, sex, ID_card, address, house_number)
        values (#{userName}, #{loginNo}, #{phoneNumber}, #{sex}, #{IDCard}, #{address}, #{houseNumber})
    
    

6、Person.java

@Slf4j
@Data
public class Person  {
 private Integer id;
 private String userName;
 private String loginNo;
 private String phoneNumber;
 private String sex;
 private String IDCard;
 private String address;
 private String houseNumber;
}

網(wǎng)站名稱:Spring Boot:如何快速集成Mybatis和Thymeleaf
文章出自:http://www.dlmjj.cn/article/dhcpccj.html