M
apper类,Mapper.xml ,Service类, 是生成的。
controller类是写的。 test测试
出错 Mapper类的参数好像传不到Mapper.xml中, 是哪一部分出错了呢?
你再Mapper.xml中是否添加: 这句话?
<mapper namespace="com.xxx.xxx.BarcodeMapper">
mapper.xml文件完整发下比较好判断问题
1.命名空间要全路径,
2.dao层接口类上要有对应的注解
感觉你mapper对象没注入,,,,空指针,,,
你看下是不是mapper对象的问题,,
service对象没有注入,所以报空指针了,你debug看下
@Service @Transactional public class BarcodeService { @Autowired private BarcodeMapper mapper; public long countByExample(BarcodeQuery example) { return mapper.countByExample(example); }
@Service
@Transactional
public class BarcodeService {
@Autowired
private BarcodeMapper mapper;
public long countByExample(BarcodeQuery example) {
return mapper.countByExample(example);
}
public int deleteByExample(BarcodeQuery example) {
return mapper.deleteByExample(example);
}
public int deleteByPrimaryKey(Integer id) {
return mapper.deleteByPrimaryKey(id);
}
//插入
public void insert(Barcode record) {
System.out.println("service: insert ---record---"+record);
mapper.insertSelective(record);
}
public int insertSelective(Barcode record) {
return mapper.insertSelective(record);
}
public java.util.List<Barcode> selectByExampleWithRowbounds(BarcodeQuery example, RowBounds rowBounds) {
return mapper.selectByExampleWithRowbounds(example,rowBounds);
}
public java.util.List<Barcode> selectByExample(BarcodeQuery example) {
return mapper.selectByExample(example);
}
public Barcode selectByPrimaryKey(Integer id) {
return mapper.selectByPrimaryKey(id);
}
// (添) 查找 prodcode查找
public int selectByProdcode(String prodcode){
System.out.println("service: selectByProdcode ---prodcode---"+prodcode);
return mapper.selectByProdcode(prodcode);
}
public int updateByExampleSelective(Barcode record, BarcodeQuery example) {
return mapper.updateByExampleSelective(record,example);
}
public int updateByExample(Barcode record, BarcodeQuery example) {
return mapper.updateByExample(record,example);
}
public int updateByPrimaryKeySelective(Barcode record) {
return mapper.updateByPrimaryKeySelective(record);
}
public int updateByPrimaryKey(Barcode record) {
return mapper.updateByPrimaryKey(record);
}
public void setDataSourceName(String dataSourceName) {
MultiDataSource.setDataSourceName(dataSourceName);
}
public void setDefaultDataSource() {
MultiDataSource.resetDataSourceName();
}
public List<Barcode> selectAll(){
return mapper.selectAll();
}
}
# 2.Mapper
public interface BarcodeMapper {
long countByExample(BarcodeQuery example);
int deleteByExample(BarcodeQuery example);
int deleteByPrimaryKey(Integer id);
//插入
void insert(Barcode record);
int insertSelective(Barcode record);
List<Barcode> selectByExampleWithRowbounds(BarcodeQuery example, RowBounds rowBounds);
List<Barcode> selectByExample(BarcodeQuery example);
Barcode selectByPrimaryKey(Integer id);
// (添) 查找 prodcode查找
int selectByProdcode(String prodcode);
int updateByExampleSelective(@Param("record") Barcode record, @Param("example") BarcodeQuery example);
int updateByExample(@Param("record") Barcode record, @Param("example") BarcodeQuery example);
int updateByPrimaryKeySelective(Barcode record);
int updateByPrimaryKey(Barcode record);
List<Barcode> selectAll();
}
# 3.mapper.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="org.publiccms.logic.mapper.tools.BarcodeMapper">
<resultMap id="BaseResultMap" type="org.publiccms.entities.sys.Barcode">
<id column="ID" jdbcType="INTEGER" property="id" />
<result column="tailId" jdbcType="INTEGER" property="tailid" />
<result column="prodcode" jdbcType="VARCHAR" property="prodcode" />
</resultMap>
<sql id="Example_Where_Clause">
<where>
<foreach collection="oredCriteria" item="criteria" separator="or">
<if test="criteria.valid">
<trim prefix="(" prefixOverrides="and" suffix=")">
<foreach collection="criteria.criteria" item="criterion">
<choose>
<when test="criterion.noValue">
and ${criterion.condition}
</when>
<when test="criterion.singleValue">
and ${criterion.condition} #{criterion.value}
</when>
<when test="criterion.betweenValue">
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
</when>
<when test="criterion.listValue">
and ${criterion.condition}
<foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
#{listItem}
</foreach>
</when>
</choose>
</foreach>
</trim>
</if>
</foreach>
</where>
</sql>
<sql id="Update_By_Example_Where_Clause">
<where>
<foreach collection="example.oredCriteria" item="criteria" separator="or">
<if test="criteria.valid">
<trim prefix="(" prefixOverrides="and" suffix=")">
<foreach collection="criteria.criteria" item="criterion">
<choose>
<when test="criterion.noValue">
and ${criterion.condition}
</when>
<when test="criterion.singleValue">
and ${criterion.condition} #{criterion.value}
</when>
<when test="criterion.betweenValue">
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
</when>
<when test="criterion.listValue">
and ${criterion.condition}
<foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
#{listItem}
</foreach>
</when>
</choose>
</foreach>
</trim>
</if>
</foreach>
</where>
</sql>
<sql id="Base_Column_List">
ID, tailId, prodcode
</sql>
<!-- 查找 ("distinct"去重)-->
<select id="selectByExample" parameterType="org.publiccms.entities.sys.BarcodeQuery" resultMap="BaseResultMap">
select
<if test="distinct">
distinct
</if>
<include refid="Base_Column_List" />
from barcode
<if test="_parameter != null">
<include refid="Example_Where_Clause" />
</if>
<if test="orderByClause != null">
order by ${orderByClause}
</if>
</select>
<!-- 查找 id查找-->
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from barcode
where ID = #{id,jdbcType=INTEGER}
</select>
<!-- 查找 prodcode查找 select from job_info group by job_id order by id desc;-->
<select id="selectByProdcode" parameterType="org.publiccms.entities.sys.Barcode" resultMap="BaseResultMap">
select max(tailId)
<include refid="Base_Column_List" />
from barcode
where prodcode = #{prodcode,jdbcType=INTEGER}
</select>
<!-- //删除-->
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
delete from barcode
where ID = #{id,jdbcType=INTEGER}
</delete>
<delete id="deleteByExample" parameterType="org.publiccms.entities.sys.BarcodeQuery">
delete from barcode
<if test="_parameter != null">
<include refid="Example_Where_Clause" />
</if>
</delete>
<!-- 插入 -->
<insert id="insert" parameterType="org.publiccms.entities.sys.Barcode">
insert into barcode (ID,tailId, prodcode)
values (#{id},#{tailid}, #{prodcode})
</insert>
<insert id="insertSelective" parameterType="org.publiccms.entities.sys.Barcode">
insert into barcode
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
ID,
</if>
<if test="tailid != null">
tailId,
</if>
<if test="prodcode != null">
prodcode,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id,jdbcType=INTEGER},
</if>
<if test="tailid != null">
#{tailid,jdbcType=INTEGER},
</if>
<if test="prodcode != null">
#{prodcode,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<!-- test="_parameter != null"测试对象: -->
<select id="countByExample" parameterType="org.publiccms.entities.sys.BarcodeQuery" resultType="java.lang.Long">
select count(*) from barcode
<if test="_parameter != null">
<include refid="Example_Where_Clause" />
</if>
</select>
<!-- 更新 -->
<update id="updateByExampleSelective" parameterType="map">
update barcode
<set>
<if test="record.id != null">
ID = #{record.id,jdbcType=INTEGER},
</if>
<if test="record.tailid != null">
tailId = #{record.tailid,jdbcType=INTEGER},
</if>
<if test="record.prodcode != null">
prodcode = #{record.prodcode,jdbcType=VARCHAR},
</if>
</set>
<if test="_parameter != null">
<include refid="Update_By_Example_Where_Clause" />
</if>
</update>
<update id="updateByExample" parameterType="map">
update barcode
set ID = #{record.id,jdbcType=INTEGER},
tailId = #{record.tailid,jdbcType=INTEGER},
prodcode = #{record.prodcode,jdbcType=VARCHAR}
<if test="_parameter != null">
<include refid="Update_By_Example_Where_Clause" />
</if>
</update>
<update id="updateByPrimaryKeySelective" parameterType="org.publiccms.entities.sys.Barcode">
update barcode
<set>
<if test="tailid != null">
tailId = #{tailid,jdbcType=INTEGER},
</if>
<if test="prodcode != null">
prodcode = #{prodcode,jdbcType=VARCHAR},
</if>
</set>
where ID = #{id,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="org.publiccms.entities.sys.Barcode">
update barcode
set tailId = #{tailid,jdbcType=INTEGER},
prodcode = #{prodcode,jdbcType=VARCHAR}
where ID = #{id,jdbcType=INTEGER}
</update>
<select id="selectByExampleWithRowbounds" parameterType="org.publiccms.entities.sys.BarcodeQuery" resultMap="BaseResultMap">
select
<if test="distinct">
distinct
</if>
<include refid="Base_Column_List" />
from barcode
<if test="_parameter != null">
<include refid="Example_Where_Clause" />
</if>
<if test="orderByClause != null">
order by ${orderByClause}
</if>
</select>
<!-- 查找 prodcode查找 select from job_info group by job_id order by id desc;-->
<select id="selectAll" resultMap="BaseResultMap">
select *
<include refid="Base_Column_List" />
from barcode
</select>
</mapper>
# 4.controller
@Controller
@RequestMapping(value = "borcodeController")
public class BorcodeController extends AbstractController{
@Autowired
BarcodeService service=new BarcodeService();
Barcode barcode=new Barcode();
@RequestMapping(value = "/insertGoodsinfo", method = RequestMethod.POST)
public void insertGoodsinfo(){
barcode.setId(9);
barcode.setProdcode("23");
barcode.setTailid(1);
service.insert(barcode);
//List<Barcode> l=service.selectAll();
System.out.println("----");
}
}
# 5.entity
package org.publiccms.entities.sys;
import static org.publiccms.common.database.CmsUpgrader.IDENTIFIER_GENERATOR;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.annotations.GenericGenerator;
import com.publiccms.common.generator.annotation.GeneratorColumn;
@Entity
@Table(name = "barcode")
public class Barcode implements Serializable {
@GeneratorColumn(title = "ID")
private Integer id;
private Integer tailid;
@GeneratorColumn(title = "产品编码")
private String prodcode;
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(generator = "cmsGenerator")
@GenericGenerator(name = "cmsGenerator", strategy = IDENTIFIER_GENERATOR)
@Column(name = "id", unique = true, nullable = false)
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getTailid() {
return tailid;
}
public void setTailid(Integer tailid) {
this.tailid = tailid;
}
public String getProdcode() {
return prodcode;
}
public void setProdcode(String prodcode) {
this.prodcode = prodcode == null ? null : prodcode.trim();
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(getClass().getSimpleName());
sb.append(" [");
sb.append("Hash = ").append(hashCode());
sb.append(", id=").append(id);
sb.append(", tailid=").append(tailid);
sb.append(", prodcode=").append(prodcode);
sb.append(", serialVersionUID=").append(serialVersionUID);
sb.append("]");
return sb.toString();
}
@Override
public boolean equals(Object that) {
if (this == that) {
return true;
}
if (that == null) {
return false;
}
if (getClass() != that.getClass()) {
return false;
}
Barcode other = (Barcode) that;
return (this.getId() == null ? other.getId() == null : this.getId().equals(other.getId()))
&& (this.getTailid() == null ? other.getTailid() == null : this.getTailid().equals(other.getTailid()))
&& (this.getProdcode() == null ? other.getProdcode() == null : this.getProdcode().equals(other.getProdcode()));
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((getId() == null) ? 0 : getId().hashCode());
result = prime * result + ((getTailid() == null) ? 0 : getTailid().hashCode());
result = prime * result + ((getProdcode() == null) ? 0 : getProdcode().hashCode());
return result;
}
}
package org.publiccms.entities.sys;
import java.util.ArrayList;
import java.util.List;
public class BarcodeQuery {
protected String orderByClause;
protected boolean distinct;
protected List<Criteria> oredCriteria;
public BarcodeQuery() {
oredCriteria = new ArrayList<Criteria>();
}
public void setOrderByClause(String orderByClause) {
this.orderByClause = orderByClause;
}
public String getOrderByClause() {
return orderByClause;
}
public void setDistinct(boolean distinct) {
this.distinct = distinct;
}
public boolean isDistinct() {
return distinct;
}
public List<Criteria> getOredCriteria() {
return oredCriteria;
}
public void or(Criteria criteria) {
oredCriteria.add(criteria);
}
public Criteria or() {
Criteria criteria = createCriteriaInternal();
oredCriteria.add(criteria);
return criteria;
}
public Criteria createCriteria() {
Criteria criteria = createCriteriaInternal();
if (oredCriteria.size() == 0) {
oredCriteria.add(criteria);
}
return criteria;
}
protected Criteria createCriteriaInternal() {
Criteria criteria = new Criteria();
return criteria;
}
public void clear() {
oredCriteria.clear();
orderByClause = null;
distinct = false;
}
protected abstract static class GeneratedCriteria {
protected List<Criterion> criteria;
protected GeneratedCriteria() {
super();
criteria = new ArrayList<Criterion>();
}
public boolean isValid() {
return criteria.size() > 0;
}
public List<Criterion> getAllCriteria() {
return criteria;
}
public List<Criterion> getCriteria() {
return criteria;
}
protected void addCriterion(String condition) {
if (condition == null) {
throw new RuntimeException("Value for condition cannot be null");
}
criteria.add(new Criterion(condition));
}
protected void addCriterion(String condition, Object value, String property) {
if (value == null) {
throw new RuntimeException("Value for " + property + " cannot be null");
}
criteria.add(new Criterion(condition, value));
}
protected void addCriterion(String condition, Object value1, Object value2, String property) {
if (value1 == null || value2 == null) {
throw new RuntimeException("Between values for " + property + " cannot be null");
}
criteria.add(new Criterion(condition, value1, value2));
}
public Criteria andIdIsNull() {
addCriterion("ID is null");
return (Criteria) this;
}
public Criteria andIdIsNotNull() {
addCriterion("ID is not null");
return (Criteria) this;
}
public Criteria andIdEqualTo(Integer value) {
addCriterion("ID =", value, "id");
return (Criteria) this;
}
public Criteria andIdNotEqualTo(Integer value) {
addCriterion("ID <>", value, "id");
return (Criteria) this;
}
public Criteria andIdGreaterThan(Integer value) {
addCriterion("ID >", value, "id");
return (Criteria) this;
}
public Criteria andIdGreaterThanOrEqualTo(Integer value) {
addCriterion("ID >=", value, "id");
return (Criteria) this;
}
public Criteria andIdLessThan(Integer value) {
addCriterion("ID <", value, "id");
return (Criteria) this;
}
public Criteria andIdLessThanOrEqualTo(Integer value) {
addCriterion("ID <=", value, "id");
return (Criteria) this;
}
public Criteria andIdIn(List<Integer> values) {
addCriterion("ID in", values, "id");
return (Criteria) this;
}
public Criteria andIdNotIn(List<Integer> values) {
addCriterion("ID not in", values, "id");
return (Criteria) this;
}
public Criteria andIdBetween(Integer value1, Integer value2) {
addCriterion("ID between", value1, value2, "id");
return (Criteria) this;
}
public Criteria andIdNotBetween(Integer value1, Integer value2) {
addCriterion("ID not between", value1, value2, "id");
return (Criteria) this;
}
public Criteria andTailidIsNull() {
addCriterion("tailId is null");
return (Criteria) this;
}
public Criteria andTailidIsNotNull() {
addCriterion("tailId is not null");
return (Criteria) this;
}
public Criteria andTailidEqualTo(Integer value) {
addCriterion("tailId =", value, "tailid");
return (Criteria) this;
}
public Criteria andTailidNotEqualTo(Integer value) {
addCriterion("tailId <>", value, "tailid");
return (Criteria) this;
}
public Criteria andTailidGreaterThan(Integer value) {
addCriterion("tailId >", value, "tailid");
return (Criteria) this;
}
public Criteria andTailidGreaterThanOrEqualTo(Integer value) {
addCriterion("tailId >=", value, "tailid");
return (Criteria) this;
}
public Criteria andTailidLessThan(Integer value) {
addCriterion("tailId <", value, "tailid");
return (Criteria) this;
}
public Criteria andTailidLessThanOrEqualTo(Integer value) {
addCriterion("tailId <=", value, "tailid");
return (Criteria) this;
}
public Criteria andTailidIn(List<Integer> values) {
addCriterion("tailId in", values, "tailid");
return (Criteria) this;
}
public Criteria andTailidNotIn(List<Integer> values) {
addCriterion("tailId not in", values, "tailid");
return (Criteria) this;
}
public Criteria andTailidBetween(Integer value1, Integer value2) {
addCriterion("tailId between", value1, value2, "tailid");
return (Criteria) this;
}
public Criteria andTailidNotBetween(Integer value1, Integer value2) {
addCriterion("tailId not between", value1, value2, "tailid");
return (Criteria) this;
}
public Criteria andProdcodeIsNull() {
addCriterion("prodcode is null");
return (Criteria) this;
}
public Criteria andProdcodeIsNotNull() {
addCriterion("prodcode is not null");
return (Criteria) this;
}
public Criteria andProdcodeEqualTo(String value) {
addCriterion("prodcode =", value, "prodcode");
return (Criteria) this;
}
public Criteria andProdcodeNotEqualTo(String value) {
addCriterion("prodcode <>", value, "prodcode");
return (Criteria) this;
}
public Criteria andProdcodeGreaterThan(String value) {
addCriterion("prodcode >", value, "prodcode");
return (Criteria) this;
}
public Criteria andProdcodeGreaterThanOrEqualTo(String value) {
addCriterion("prodcode >=", value, "prodcode");
return (Criteria) this;
}
public Criteria andProdcodeLessThan(String value) {
addCriterion("prodcode <", value, "prodcode");
return (Criteria) this;
}
public Criteria andProdcodeLessThanOrEqualTo(String value) {
addCriterion("prodcode <=", value, "prodcode");
return (Criteria) this;
}
public Criteria andProdcodeLike(String value) {
addCriterion("prodcode like", value, "prodcode");
return (Criteria) this;
}
public Criteria andProdcodeNotLike(String value) {
addCriterion("prodcode not like", value, "prodcode");
return (Criteria) this;
}
public Criteria andProdcodeIn(List<String> values) {
addCriterion("prodcode in", values, "prodcode");
return (Criteria) this;
}
public Criteria andProdcodeNotIn(List<String> values) {
addCriterion("prodcode not in", values, "prodcode");
return (Criteria) this;
}
public Criteria andProdcodeBetween(String value1, String value2) {
addCriterion("prodcode between", value1, value2, "prodcode");
return (Criteria) this;
}
public Criteria andProdcodeNotBetween(String value1, String value2) {
addCriterion("prodcode not between", value1, value2, "prodcode");
return (Criteria) this;
}
}
public static class Criteria extends GeneratedCriteria {
protected Criteria() {
super();
}
}
public static class Criterion {
private String condition;
private Object value;
private Object secondValue;
private boolean noValue;
private boolean singleValue;
private boolean betweenValue;
private boolean listValue;
private String typeHandler;
public String getCondition() {
return condition;
}
public Object getValue() {
return value;
}
public Object getSecondValue() {
return secondValue;
}
public boolean isNoValue() {
return noValue;
}
public boolean isSingleValue() {
return singleValue;
}
public boolean isBetweenValue() {
return betweenValue;
}
public boolean isListValue() {
return listValue;
}
public String getTypeHandler() {
return typeHandler;
}
protected Criterion(String condition) {
super();
this.condition = condition;
this.typeHandler = null;
this.noValue = true;
}
protected Criterion(String condition, Object value, String typeHandler) {
super();
this.condition = condition;
this.value = value;
this.typeHandler = typeHandler;
if (value instanceof List<?>) {
this.listValue = true;
} else {
this.singleValue = true;
}
}
protected Criterion(String condition, Object value) {
this(condition, value, null);
}
protected Criterion(String condition, Object value, Object secondValue, String typeHandler) {
super();
this.condition = condition;
this.value = value;
this.secondValue = secondValue;
this.typeHandler = typeHandler;
this.betweenValue = true;
}
protected Criterion(String condition, Object value, Object secondValue) {
this(condition, value, secondValue, null);
}
}
}
@Entity
@Table(name = "barcode")
public class Barcode implements Serializable {
@GeneratorColumn(title = "ID")
private Integer id;
private Integer tailid;
@GeneratorColumn(title = "产品编码")
private String prodcode;
我觉得可能和你实体类里的这一块有关。
insert into barcode (ID,tailId, prodcode)
values (#{id},#{tailid}, #{prodcode})
这里的#{id},#{tailid}, #{prodcode}和你数据库表中的列名是对应的吗?你这个实体类我有点不太懂,接触不多,只是看过一次。如果楼主方便的话,
可以解释下@GeneratorColumn(title = "产品编码")这个注解是什么意思吗?。
还有如果想要在控制台打印sql语句的话,可以在你mybatis的configuration.xml文件中添加
<!-- 打印查询语句 -->
这行语句(需要重启服务器)
这样你就能确定是不是没有传参数或者哪个参数有问题了。
<!-- 打印查询语句 -->
忽然发现我的打印语句没了,重发一遍。
<!-- 打印查询语句 -->
啥情况。。。再发一遍
<setting name="logImpl" value="STDOUT_LOGGING" />
原来要用代码片的格式发才显示。。。。