提交myvatis

This commit is contained in:
qiurunze123 2019-01-19 22:59:38 +08:00
parent a2eb664f45
commit 3a81c250d6
12 changed files with 176 additions and 105 deletions

View File

@ -161,6 +161,10 @@
<version>1.18.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
</dependency>
</dependencies>

View File

@ -1,5 +1,6 @@
package com.geekq.miaosha;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

View File

@ -42,9 +42,13 @@ public class AccessInterceptor extends HandlerInterceptorAdapter{
*/
if(handler instanceof HandlerMethod) {
logger.info("打印拦截方法handler {} ",handler);
HandlerMethod hm = (HandlerMethod)handler;
//方便mybatis 测试
if(hm.getMethod().getName().startsWith("test")){
return true;
}
MiaoshaUser user = getUser(request, response);
UserContext.setUser(user);
HandlerMethod hm = (HandlerMethod)handler;
AccessLimit accessLimit = hm.getMethodAnnotation(AccessLimit.class);
if(accessLimit == null) {
return true;

View File

@ -0,0 +1,18 @@
package com.geekq.miaosha.mybatis.Mapper;
import com.geekq.miaosha.mybatis.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
@Mapper
public interface UserMapper {
public User getUser(Integer id);
public int insert(User user);
public List<User> getUserList(@Param("id") Integer id , @Param("name") String name);
}

View File

@ -1,17 +0,0 @@
package com.geekq.miaosha.mybatis.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/message")
public class MessageController {
@RequestMapping("index")
public String index(){
return "message";
}
}

View File

@ -0,0 +1,54 @@
package com.geekq.miaosha.mybatis.controller;
import com.geekq.miaosha.access.AccessLimit;
import com.geekq.miaosha.mybatis.Mapper.UserMapper;
import com.geekq.miaosha.mybatis.entity.User;
import com.geekq.miaosha.redis.KeyPrefix;
import com.geekq.miaosha.redis.RedisService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.context.WebContext;
import org.thymeleaf.spring4.view.ThymeleafViewResolver;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.List;
@Controller
@RequestMapping("/test")
public class UbatisController {
@Autowired
private UserMapper userMapper ;
@RequestMapping(value = "/testUser", produces = "text/html")
@ResponseBody
public void testMybatis(){
User user = userMapper.getUser(1);
System.out.println(user.getId());
}
@RequestMapping(value = "/testSelectLIst", produces = "text/html")
@ResponseBody
public void testSelectUser(){
List<User> result = userMapper.getUserList(1,"xiaoming");
System.out.println(result.size());
}
@RequestMapping(value = "/testInsert", produces = "text/html")
@ResponseBody
public void testInsert(){
User user =new User();
user.setName("xiaoming");
user.setAge(16);
int result = userMapper.insert(user);
System.out.println(result);
}
}

View File

@ -0,0 +1,24 @@
package com.geekq.miaosha.mybatis.entity;
import lombok.Getter;
import lombok.Setter;
/**
* @author qiurunze
* users表所对应的实体类
*/
@Setter
@Getter
public class User {
//实体类的属性和表的字段名称一一对应
private int id;
private String name;
private int age;
private String address;
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}

View File

@ -16,11 +16,11 @@ mybatis.configuration.map-underscore-to-camel-case=true
#mybatis.mapperLocations = classpath:com/geekq/miaosha/dao/*.xml
mybatis.mapperLocations=classpath:mybatis/mapper/*.xml
#add mybatis
mybatis.config-locations=classpath:mybatis/config.xml
mybatis.config-locations=classpath:mybatis/conf.xml
#datasource
spring.datasource.url=jdbc:mysql://localhost:3306/miaosha?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false
spring.datasource.username=root
spring.datasource.password=xxxxx
spring.datasource.password=aixiyue11
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#druid
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

View File

@ -0,0 +1,8 @@
<?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>
<mappers>
<mapper resource="com.geekq.miaosha.mybatis.Mapper.UserMapper"/>
</mappers>
</configuration>

View File

@ -1,11 +0,0 @@
<?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>
<typeAlias alias="miaoshauser1" type="com.geekq.miaosha.domain.MiaoshaUser"/>
</typeAliases>
<!-- <mappers>
<mapper resource="/mybatis/mapper/UserMapper.xml"></mapper>
</mappers>-->
</configuration>

View File

@ -1,74 +0,0 @@
<?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.geekq.miaosha.dao.UserMapper">
<!--结果集映射-->
<resultMap id="BaseResultMap" type="com.geekq.miaosha.domain.MiaoshaUser">
<id column="id" property="id" jdbcType="BIGINT"/>
<result column="nickname" property="nickname" jdbcType="VARCHAR"/>
<result column="salt" property="salt" jdbcType="VARCHAR"/>
<result column="head" property="head" jdbcType="VARCHAR"/>
<result column="register_date" property="registerDate" jdbcType="TIMESTAMP"/>
<result column="last_login_date" property="lastLoginDate" jdbcType="TIMESTAMP"/>
<result column="login_Count" property="loginCount" jdbcType="INTEGER"/>
</resultMap>
<!--增加一条数据-->
<insert id="insertMiaoShaUser" parameterType="com.geekq.miaosha.domain.MiaoshaUser" keyProperty="id">
insert into miaosha_user (id, nickname, salt, register_date,last_login_date,login_Count ) values
(#{id,jdbcType=BIGINT},#{nickname,jdbcType=VARCHAR},
#{salt,jdbcType=VARCHAR},#{registerDate,jdbcType=DATE},#{lastLoginDate,jdbcType=DATE},
#{loginCount,jdbcType=INTEGER}
)
</insert>
<!--批量插入-->
<insert id="insertMiaoShaUserValues" useGeneratedKeys="true"
keyProperty="id">
insert into miaosha_user (id, nickname, salt, register_date,last_login_date,login_Count ) values
<foreach item="item" collection="list" separator=",">
(#{item.id,jdbcType=BIGINT},#{item.nickname,jdbcType=VARCHAR},
#{item.salt,jdbcType=VARCHAR},#{item.registerDate,jdbcType=DATE},#{item.lastLoginDate,jdbcType=DATE},
#{item.loginCount,jdbcType=INTEGER}
)
</foreach>
</insert>
<!--更新一条数据-->
<update id="updateMiaoShaUser" >
update miaosha_user set nickname = #{nickname},
login_Count = #{loginCount}
where id=#{id}
</update>
<!--删除一条数据-->
<delete id="deleteMiaoShaUser" parameterType="Long">
delete from miaosha_user
where id = #{id,jdbcType=BIGINT}
</delete>
<sql id="Base_Column_List"> id, nickname, salt, register_date,last_login_date,login_Count </sql>
<select id="getMiaoShaUserById" parameterType="LONG" resultType="miaoshauser">
SELECT
<include refid="Base_Column_List"/>
FROM miaosha_user WHERE id = #{id}
</select>
<select id="getMiaoShaUserNum" resultType="int">
select count(*) from miaosha_user
</select>
<select id="countbyMenuId" parameterType="java.lang.Integer"
resultType="java.lang.Integer"> select count(*) from miaosha_user
</select>
</mapper>

View File

@ -0,0 +1,60 @@
<?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指定一个唯一的namespacenamespace的值习惯上设置成包名+sql映射文件名这样就能够保证namespace的值是唯一的
例如 namespace="com.geekq.uMybatis.mapping.UserMapper"就是 com.geekq.uMybatis.mapping.UserMapper(包名)+
userMapper(userMapper.xml文件去除后缀)保证唯一性
-->
<mapper namespace="com.geekq.miaosha.mybatis.Mapper.UserMapper">
<!-- 在select标签中编写查询的SQL语句 设置select标签的id属性为getUserid属性值必须是唯一的不能够重复
使用parameterType属性指明查询时使用的参数类型resultType属性指明查询返回的结果集类型
resultType="me.gacl.domain.User"就表示将查询结果封装成一个User类的对象返回
User类就是users表所对应的实体类
-->
<!--
根据id查询得到一个user对象
-->
<resultMap type="com.geekq.miaosha.mybatis.entity.User" id="userResultMap">
<!-- id表示查询结果集中唯一标识 column:查询出的列名
property:type所指定的POJO中的属性名
最终reslutMap对column和property做一个映射关系(对应关系)
-->
<id column="id" property="id"/>
<!-- 对普通列的映射定义 -->
<result column="name" property="name"/>
<result column="age" property="age"/>
<result column="_address" property="address"/>
</resultMap>
<sql id="base_column">
id,name,age,_address
</sql>
<!-- 查询-->
<select id="getUser" parameterType="int"
resultType="com.geekq.miaosha.mybatis.entity.User">
select * from users where id=#{id}
</select>
<select id="getUserList" resultType="com.geekq.miaosha.mybatis.entity.User">
select <include refid="base_column" />
from users where 1=1
<if test="id != null and id !=''">
and id=#{id}
</if>
<if test="name != null and name !=''">
and name=#{name}
</if>
</select>
<!-- 插入自动递增-->
<insert id="insert" parameterType="com.geekq.miaosha.mybatis.entity.User"
useGeneratedKeys="true" keyProperty="id">
insert users (id,name,age) value (#{id},#{name},#{age})
</insert>
</mapper>