韦德国际_韦德国际1946官方网站_韦德国际1946手机版
做最好的网站

记springboot mybatis freemarker bootstrap的利用(二)

日期:2019-05-22编辑作者:韦德国际1946官方网站

二、springboot mybatis的使用

创建maven项目后,在pox.xml中丰硕依赖的jar包

韦德国际1946官方网站 1韦德国际1946官方网站 2

<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>springboot-mybatis</groupId>
<artifactId>springboot-mybatis</artifactId>
<version>1.0.0</version>
<packaging>war</packaging>
<name>springBoot-mybatis</name>
<description>Spring Boot project</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.2.RELEASE</version>
<relativePath />
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.36</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>

</dependencies>

<build>
<finalName>SpringMybatis</finalName>
<plugins>
<!-- define the project compile level -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>

<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
</project>

View Code

创办maven项目后,在pox.xml中增进依赖的jar包

韦德国际1946官方网站 3韦德国际1946官方网站 4

<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>springboot-mybatis</groupId>
<artifactId>springboot-mybatis</artifactId>
<version>1.0.0</version>
<packaging>war</packaging>
<name>springBoot-mybatis</name>
<description>Spring Boot project</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.2.RELEASE</version>
<relativePath />
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.36</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>

</dependencies>

<build>
<finalName>SpringMybatis</finalName>
<plugins>
<!-- define the project compile level -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>

<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
</project>

韦德国际1946官方网站,View Code

springboot下使用mybatis

采纳mybatis-spring-boot-starter就能够。 简单的讲便是mybatis看见spring boot这么火,于是搞出来mybatis-spring-boot-starter那个化解方案来与springboot更加好的三合1

记springboot mybatis freemarker bootstrap的利用(二)。详见

http://www.mybatis.org/spring/zh/index.html

     一.springboot的疏解:@SpringBootApplication :运转项目:整合常用评释(@Configuration,@EnableAutoConfiguration,@ComponentScan)/扫包成效(只可以在当下同级包下)

新建3个Application类,用来运维web项目

package com.hushunwei;

import org.apache.log4j.Logger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {
private static Logger logger = Logger.getLogger(Application.class);

public static void main(String[] args) {
// TODO Auto-generated method stub

SpringApplication.run(Application.class, args);

logger.info("============= SpringBoot Start Success =============");
}

}

新建3个Application类,用来运维web项目

package com.hushunwei;

import org.apache.log4j.Logger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {
private static Logger logger = Logger.getLogger(Application.class);

public static void main(String[] args) {
// TODO Auto-generated method stub

SpringApplication.run(Application.class, args);

logger.info("============= SpringBoot Start Success =============");
}

}

引入mybatis-spring-boot-starter的pom文件

<dependency>   

    <groupId>org.mybatis.spring.boot</groupId>   

    <artifactId>mybatis-spring-boot-starter</artifactId>   

    <version>1.1.1</version>   

</dependency>

                @EnableAutoConfiguration自动配置

新建三个application.properties配置文件

spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
server.port=8080

 

新建2个application.properties配置文件

spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
server.port=8080

 

application.properties 增多辅车相依铺排

spring.datasource.driverClassName = com.mysql.jdbc.Driver

spring.datasource.url = jdbc:mysql://localhost:3306/city?useUnicode=true&characterEncoding=utf-8

spring.datasource.username = root

spring.datasource.password = mysql

springboot会自动加载spring.datasource.*连带安插,数据源就能够自行注入到sqlSessionFactory中,sqlSessionFactory会自动注入到Mapper中,对了您一切都无须管了,直接拿起来使用就行了。

mybatis.type-aliases-package=com.test.demo.model

这几个布局用来内定bean在哪个包里,防止存在同名class时找不到bean

在开行类中增多@MapperScan钦赐dao只怕mapper包的地方,能够用 {"",""}的样式内定七个包

@SpringBootApplication

@MapperScan("com.test.demo.dao")

public class Application {

    public static void main(String[] args) {

        SpringApplication.run(Application.class, args);

    }

}

依旧直接在Mapper类上边增添表明@Mapper也得以钦赐mapper,提议使用方面这种,给各类mapper加个申明挺劳顿不说,假若是dao的包,照旧要用@MapperScan来钦赐地方

接下去,能够用注脚形式开采mapper,或许用xml方式开垦

                @ComponentScan扫描一些零件如controller、service 能够扫多个包。扫多少个包用法@ComponentScan(basePackages={"com.zty.controller","com.zty.service"})

 新建多个实体类(数据库表和字段本人创立)

package com.hushunwei.pojo;

public class User {

private int id;
private String name;
private String pwd;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
}

 新建二个实体类(数据库表和字段本身创办)

package com.hushunwei.pojo;

public class User {

private int id;
private String name;
private String pwd;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
}

申明形式

@Mapper

public interface CityMapper {

    @Select("select * from city where state = #{state}")

    City findByState(@Param("state") String state);

}

@Select 是查询类的批注,全数的询问均接纳那几个 @Result 修饰重临的结果集,关联实体类属性和数据库字段壹一对应,假使实体类属性和数据库属性名保持1致,就没有需求那性子子来修饰。 @Insert 插入数据库使用,间接传入实体类会自动分析属性到对应的值 @Update 担当修改,也足以直接传入对象 @delete 负担删除 领悟更多表明参照他事他说加以侦查这里

http://www.mybatis.org/mybatis-3/zh/java-api.html

                @EntityScan扫描实体类的包

新建一个mapper接口类

sql与语句用的注释达成,注意变量名要和数据库字段一样,否则就要增添@Results注解修改字段名

package com.hushunwei.dao;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import com.hushunwei.pojo.User;

@Mapper
public interface UserMapper {
@Select("select * from user where name=#{name}")
@Results({
@Result(column="id", property="id"),
@Result(column="name", property="name"),
@Result(column="pwd", property="pwd")
})
User findUserByName(@Param("name")String name);
}

新建2个mapper接口类

sql与语句用的笺注完成,注意变量名要和数据库字段相同,不然将要增多@Results评释修改字段名

package com.hushunwei.dao;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import com.hushunwei.pojo.User;

@Mapper
public interface UserMapper {
@Select("select * from user where name=#{name}")
@Results({
@Result(column="id", property="id"),
@Result(column="name", property="name"),
@Result(column="pwd", property="pwd")
})
User findUserByName(@Param("name")String name);
}

xml模式

xml方式保持映射文件的老古板,application.properties要求新扩展

mybatis.mapper-locations=classpath:mybatis/mapper/*.xml

钦定mybatis的映射xml文件地点其它,还是能够钦点mybatis的配备文件,固然急需追加mybatis的有个别基础配置,能够扩充下边包车型客车布署

mybatis.config-locations=classpath:mybatis/mybatis-config.xml

点名mybatis基础配置文件

mybatis-config.xml可以增加一些mybatis基础的布置,举例

<configuration>

    <typeAliases>

        <typeAlias alias="Integer" type="java.lang.Integer" />

        <typeAlias alias="Long" type="java.lang.Long" />

        <typeAlias alias="HashMap" type="java.util.HashMap" />

        <typeAlias alias="LinkedHashMap" type="java.util.LinkedHashMap" />

        <typeAlias alias="ArrayList" type="java.util.ArrayList" />

        <typeAlias alias="LinkedList" type="java.util.LinkedList" />

    </typeAliases>

</configuration>

编纂Dao层的代码

public interface CityDao {

    public City selectCityByState(String State);

}

对应的xml映射文件

<!DOCTYPE mapper

PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.test.demo.dao.CityDao">

    <select id="selectCityByState" parameterType="String" resultType="City">

        select * from city where state = #{state}

    </select>

</mapper>

                @MapperScan扫描Mapper类的包

新建2个Controller类

package com.hushunwei.controller;

import java.util.Date;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.hushunwei.dao.UserMapper;
import com.hushunwei.pojo.User;

@Controller
@RequestMapping({"/home"})
public class UserController {
private Logger logger = Logger.getLogger(UserController.class);
@Autowired
UserMapper userMapper;

@RequestMapping("/user")
public String user(Map<String, Object> model) {
User user = userMapper.findUserByName("admin");
if(user != null){
logger.info("user.getAge" user);
}
model.put("name", user.getName());
model.put("password", user.getPwd());
return "user";
}
}

新建3个Controller类

package com.hushunwei.controller;

import java.util.Date;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.hushunwei.dao.UserMapper;
import com.hushunwei.pojo.User;

@Controller
@RequestMapping({"/home"})
public class UserController {
private Logger logger = Logger.getLogger(UserController.class);
@Autowired
UserMapper userMapper;

@RequestMapping("/user")
public String user(Map<String, Object> model) {
User user = userMapper.findUserByName("admin");
if(user != null){
logger.info("user.getAge" user);
}
model.put("name", user.getName());
model.put("password", user.getPwd());
return "user";
}
}

                @Controller 注明那是1个controller组件

在resource目录下新建四个templates文件夹,在文件下建2个user.ftl文件

<html>
<body>
账号:${name}<br/>
密码:${password}
</body>
</html>

在resource目录下新建一个templates文件夹,在文书下建2个user.ftl文件

<html>
<body>
账号:${name}<br/>
密码:${password}
</body>
</html>

                @Service注脚那是三个service组件

测试结果,在浏览器输入

韦德国际1946官方网站 5

韦德国际1946官方网站 6

测试结果,在浏览器输入

韦德国际1946官方网站 7

韦德国际1946官方网站 8

                @Mapper评释那是一个Mybatis中的Mapper组件

                @Autowired自动加载

本文由韦德国际发布于韦德国际1946官方网站,转载请注明出处:记springboot mybatis freemarker bootstrap的利用(二)

关键词: Mybatis 所有随笔 编程语言 spingboot freemarker