日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

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

新聞中心

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

前言

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

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

目標(biāo)

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

圖片

環(huán)境配置

jdk版本:1.8

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

springboot:2.3.9.RELEASE

具體步驟

依賴(lài)引入

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


    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ù)庫(kù)連接、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ù)庫(kù)連接配置
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


登陸名:
姓名:
性別:
手機(jī)號(hào)碼:
身份證號(hào):
地址:
門(mén)牌號(hào):

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




    
    主頁(yè)
    


ID:

登陸名:

姓名:

性別:

手機(jī)號(hào)碼:

身份證號(hào):

地址:

門(mén)牌號(hào):

后端代碼

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;
}

當(dāng)前標(biāo)題:Spring Boot:如何快速集成Mybatis和Thymeleaf
文章位置:http://www.5511xx.com/article/dhcpccj.html