springboot helloworld搭建:html
1:建立meaven項目,以下圖:java
2:編寫pom.xmlmysql
<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.hjj</groupId>
<artifactId>springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<build>
<finalName>bootdemo</finalName>
<plugins>
<plugin>
<inherited>true</inherited>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>${compiler.source}</source>
<target>${compiler.target}</target>
<encoding>${project.build.sourceEncoding}</encoding>
<compilerArguments>
<extdirs>${project.basedir}/src/main/webapp/WEB-INF/lib</extdirs>
</compilerArguments>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<compiler.source>1.7</compiler.source>
<compiler.target>1.7</compiler.target>web
<!-- servlet/jsp/EL (2.4/2.0/?)(2.5/2.1/2.1),(3.0/2.2/2.2),(3.1/2.3/3.0) -->
<servlet.version>3.1.0</servlet.version>
<jsp.version>2.3.1</jsp.version>
<jstl.version>1.2</jstl.version>
<junit.version>4.12</junit.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${servlet.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>${jsp.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>${jstl.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>ajax
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl-api</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.objenesis</groupId>
<artifactId>objenesis</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>spring
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
<scope>runtime</scope>
</dependency>
<!-- freemarker jar包導入 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>sql
<dependency>
<groupId>com.datastax.cassandra</groupId>
<artifactId>cassandra-driver-mapping</artifactId>
</dependency>
</dependencies>
</project>數據庫
3:在src/main/resources 在建立資源配置文件application.propertiesapache
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/springboot <!--配置數據庫-->
spring.datasource.username=root
spring.datasource.password=密碼api
spring.mvc.view.prefix: /WEB-INF/jsp/ <!--配置springmvc 中controller返回的jsp頁面路徑-->
spring.mvc.view.suffix: .jsp
mybatis.typeAliasesPackage=cn.entity <!--配置實體映射 到 mybatis 路徑位置根據本身項目而定-->
mybatis.mapperLocations=classpath*:cn/mapper/UserMapper.xml
## Freemarker \u914D\u7F6E
## \u6587\u4EF6\u914D\u7F6E\u8DEF\u5F84
spring.freemarker.template-loader-path=classpath:/templates/
spring.freemarker.cache=false
spring.freemarker.charset=UTF-8
spring.freemarker.check-template-location=true
spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=true
spring.freemarker.expose-session-attributes=true
spring.freemarker.request-context-attribute=request
spring.freemarker.suffix=.ftl
4:編寫實體類 entity User:
import java.io.Serializable;
public class User implements Serializable{
private static final long serialVersionUID = 1L;
private Integer id;
private String name;
private String password;
public User() {
}
public User(Integer id, String name, String password) {
super();
this.id = id;
this.name = name;
this.password = password;
}
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 getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "[name"+name+",password"+password+"]";
}
}
5:編寫mapper UserMapper:
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface UserMapper {
//增長
void add(User u);
//刪除
void delete(Integer id);
//修改
void update(User u);
//查詢
User get(Integer id);
}
6:編寫UserMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.mapper.UserMapper">
<!--增長 void add(User u) -->
<insert id="add" parameterType="cn.entity.User">
insert into t_user (name,passpord) values (#{name},#{password});
</insert>
<!-- 刪除delete(Integer id) -->
<delete id="delete" parameterType="Integer">
delete from t_user where id=#{v}
</delete>
<!--修改 update(User u) -->
<update id="update" parameterType="cn.entity.User">
update t_user set name=#{name},password=#{password} where id=#{id}
</update>
<!--查詢 User get(Integer id) -->
<select id="get" parameterType="Integer" resultType="cn.entity.User">
select * from t_user where id=#{v}
</select>
</mapper>
7:編寫 service UserService:
public interface UserService {
//增長
void add(User u);
//刪除
void delete(Integer id);
//修改
void update(User u);
//查詢
User get(Integer id);
}
7:編寫 service UserServiceImpl:
@ComponentScan({"cn.mapper"})
@Service("userService")
public class UserServiceImpl implements UserService{
@Resource
private UserMapper userMapper;
@Override
public void add(User u) {
userMapper.add(u);
}
@Override
public void delete(Integer id) {
userMapper.delete(id);
}
@Override
public void update(User u) {
userMapper.update(u);
}
@Override
public User get(Integer id) {
return userMapper.get(id);
}
}
8:編寫controller UserController
@Controller
@ComponentScan({"cn.service"})
@MapperScan("cn.mapper")
@EnableAutoConfiguration
public class UserController {
@Resource
private UserService userService;
@RequestMapping("/hello")
@ResponseBody
public String hello() {
return "springboot hello";
}
@RequestMapping("/find")
public String find(Model model) {
User user = userService.get(1);
model.addAttribute("user", user);
return "index";
}
@RequestMapping("/ajax")
@ResponseBody
public String ajax() {
return "{'message':'hhhhhh'}";
}
}
9:springboot程序入口: HelloApplication
@SpringBootApplication //spring Boot項目核心註解,主要目的是開啓自動配置
@Controller //標明這是一個springmvc的controller控制器
public class HelloApplication {
//在main方法中啓動一個應用,這個是入口
public static void main(String[] args) {
SpringApplication.run(UserController.class, args);
}
}
10:數據庫:
訪問http://localhost:8080/find運行結果以下: