<!-- Springboot 整合MyBatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- Druid 连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.9</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<!-- 分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.2</version>
</dependency>
spring:
http:
encoding:
force: true
charset: utf-8
enabled: true
profiles:
active: test,dev
datasource:
name: test
url: jdbc:mysql://localhost:3306/test
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
druid:
max-active: 20
filters: stat
max-wait: 60000
initial-size: 1
server:
tomcat:
uri-encoding: utf-8
port: 8888
servlet:
#项目访问路径
context-path: /ssl
application-display-name: application
#项目访问路径
path: /service
#loging日志文件位置
logging:
config: classpath:logback-spring.xml
#mybatis 配置
mybatis:
mapper-locations: classpath:mapper/*Mapper.xml
config-location: classpath:mybatis/MyBatisConfig.xml
#配置包 下面的POJO 类别名
type-aliases-package: com.feifan.pojo
#分页插件
pagehelper:
helperDialect: mysql
reasonable: true
supportMethodsArguments: true
params: count=countSql
<?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.feifan.dao.UserMapper">
<!-- <resultMap type="userMap" id="com.feifan.pojo.User">
<id column="id" property="id"/>
</resultMap> -->
<select id="selectAll" resultType="UserPOJO">
select * from user
</select>
<select id="selectById" resultType="UserPOJO">
select * from user where id = #{id}
</select>
<insert id="insertUserPOJO" parameterType="UserPOJO" >
insert into user (username,gender,age)values(#{username},#{gender},#{age})
</insert>
<update id="updateUser" parameterType="UserPOJO">
update user set username=#{username},gender=#{gender},age=#{age} where id=#{id}
</update>
</mapper>
public interface UserService {
//查找用户
UserPOJO selectById(int id);
//增加用户
void addUser(UserPOJO userPOJO);
//分页查找用户
List<UserPOJO> selectAll(int page,int rows);
//更新用户
void update(UserPOJO userPOJO);
}
@MapperScan(basePackages= {"com.feifan.dao"})//mapper dao层 单独配置扫描
<?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>
<settings>
<!-- 开启驼峰匹配 -->
<setting name="mapUnderscoreToCamelCase" value="true" />
</settings>
</configuration>
applcation.properties
spring.datasource.drive-class-name=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.url=jdbc:mysql://localhost:3306/chat
#处理浏览器响应乱码
spring.http.encoding.force=true
启动类:
//@MapperScan("com.feifan.dao")//接口扫描 在接口上标准 @mapper 此处可以省略
@Mapper//@MapperScan("com.feifan.dao")//接口扫描
public interface UserMapper {
@Select("select * from tb_user where id=#{id}")
User findUserById( Integer id);
@Insert("insert into tb_user (name,pw,age,email,gender,signtime,create_who)values(#{name},#{pw},#{age},#{email},#{gender},#{signtime},#{createWho})")
void insertUser(User user);
@Insert("insert into tb_user (name,pw)values(#{name},#{pw})")
void insertUserByName(@Param("name")String name,@Param("pw")String pw);
}