maven的依赖配置
mybatis-config.xml的配置
UserDao的Mapper
Dao实现类
最重要的Application类
<!-- spring boot 版本号 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.0.RELEASE</version>
</parent>
<!-- 依赖 -->
<dependencies>
<!-- spring boot web包 包含Spring MVC/SLF4j/Jackson/Hibernate Validator等依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- 不使用默认的默认的日志框架 Logback -->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 支持freemarker作为模板引擎 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<!-- 支持使用jdbc访问数据库 -->
<!-- <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency> -->
<!-- 支持log4j -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j</artifactId>
</dependency>
<!-- 支持aop编程 包含spring-aop 和 AspectJ -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<!-- 包含常用的测试所需的依赖,如 JUnit、Hamcrest、Mockito 和 spring-test 等。 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!-- 自动应用代码更改到最新的App上面(热部署) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<!-- 使用tomcat作为服务器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<!-- redis依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
</dependency>
<!-- mybatis依赖 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.0.0</version>
</dependency>
<!-- 其他依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.28</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
mysql驱动哪儿会提示一个5.1.37版本的驱动被覆盖,当我不配置这个依赖的时候,项目会提示找不到mysql的驱动,这个我不确定是什么原因.
我的依赖并不是最简依赖,是我找到的一个比较全的配置,并且有注解,可以根据自己的需求进行删减.
application.yml的配置文件 此配置文件需要放在classpath目录下
# Server settings
server:
port: 8080
address: localhost
contextPath: /boot-mybatis
# SPRING PROFILES
spring:
# HTTP ENCODING
http:
encoding.charset: UTF-8
encoding.enable: true
encoding.force: true
datasource:
url: jdbc:mysql://***.***.***.***:3306/test?useUnicode=true&characterEncoding=utf-8
username: ***
password: ***
max-active: 100
max-idle: 10
min-idle: 5
mybatis-config.xml的配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<package name="com.liutong.application.domain"/>
</typeAliases>
<mappers>
<mapper resource="com/liutong/application/dao/UserDao.xml" />
</mappers>
</configuration>
这儿有个注意的地方是,我的mapper.xml虽然放置在了src/main/resources下,但是路径是和Dao的接口在同一目录下,而且名字也是相同的.
实体类
package com.liutong.application.domain;
public class User {
private Long id;
private String username;
private String password;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", password="
+ password + "]";
}
}
UserDao的Mapper
<?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="com.liutong.application.dao.UserDao">
<resultMap type="com.liutong.application.domain.User" id="User">
<id property="id" column="id" javaType="java.lang.Long"/>
<result property="username" column="username" javaType="java.lang.String"/>
<result property="password" column="password" javaType="java.lang.String"/>
</resultMap>
<select id="queryUserById" parameterType="java.lang.Integer" resultMap="User">
select id,username,password from user
<where>
id = #{id}
</where>
</select>
</mapper>
Dao实现类
package com.liutong.application.dao.impl;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.liutong.application.dao.UserDao;
import com.liutong.application.domain.User;
@Repository
public class UserDaoImpl implements UserDao{
@Autowired
private SqlSessionTemplate sqlSessionTemplate;
public User queryUserById(Long id){
return sqlSessionTemplate.selectOne("queryUserById",id);
}
}
最重要的Application类
package com.liutong.application;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableAutoConfiguration
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
我能想到的,需要放上来的就都放上来了,为了怕有些遗漏,我把整个demo也打包上传了,链接在后面
---------------------------- 分割线 --------------------------
很久没有上CSDN了,看到有人评价我的源码,打的是jar包,无法看到代码,我深表歉意,当年我对这些也分不太清楚。。。
我已经将代码传到了这儿,有问题可以和我沟通