直接贴代码先:
pom:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>demo</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
application.properties:
# datasource tomcate-jdbc spring.datasource.url=jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncording=UTF-8 spring.datasource.username=root spring.datasource.password=root # freemarker spring.freemarker.suffix=.html spring.freemarker.template-loader-path=classpath:/templates
Controller:
package com.example.demo.controller; import com.example.demo.entity.UserInfo; import com.example.demo.service.UserInfoService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; /** * Created by lpw'ASUS on 2018/6/4. */ @Controller @RequestMapping("/user") public class UserInfoController { @Autowired private UserInfoService userInfoService; @RequestMapping("index") public String index(){ return "index"; } @RequestMapping("getUser") public String getUser(Model model,@RequestParam String id){ int id1 = Integer.valueOf(id); UserInfo userInfo = userInfoService.getUserInfo(id1); System.out.println("====="+userInfo); model.addAttribute("userInfo",userInfo); return "index"; } }
service层:
package com.example.demo.service; import com.example.demo.entity.UserInfo; /** * Created by lpw'ASUS on 2018/6/4. */ public interface UserInfoService { UserInfo getUserInfo(int id); }
package com.example.demo.service; import com.example.demo.dao.UserInfoDao; import com.example.demo.entity.UserInfo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; /** * Created by lpw'ASUS on 2018/6/4. */ @Service public class UserInfoServiceImpl implements UserInfoService { @Autowired private UserInfoDao userInfoDao; @Override public UserInfo getUserInfo(int id) { return userInfoDao.getUserInfo(id); } }
在实现类里面后来发现@Autowired发现报错,说不能注入,我用的idea,没有理他可以用。
dao层:
package com.example.demo.dao; import com.example.demo.entity.UserInfo; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Result; import org.apache.ibatis.annotations.Results; import org.apache.ibatis.annotations.Select; /**JpaRepository<对象,主键类型> * Created by lpw'ASUS on 2018/5/31. */ @Mapper public interface UserInfoDao{ @Select("select * from user_info where id = #{id}") @Results( value = { @Result(column = "id",property = "id"), @Result(column = "name",property = "name"), @Result(column = "sex",property = "sex") } ) public UserInfo getUserInfo(Integer id); }
bean:
package com.example.demo.entity; /** * Created by lpw'ASUS on 2018/5/30. */ public class UserInfo { private Integer id; private String name; private String sex; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } @Override public String toString() { return "UserInfo{" + "id=" + id + ", name='" + name + '\'' + ", sex='" + sex + '\'' + '}'; } }
html:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>index界面</h1> <a href="/user/getUser?id=3">这是个超链接</a> <#if userInfo??> 用户名:${userInfo.name} </#if> </body> </html>