发表于: 2017-05-27 21:26:15

1 1399


今日完成

继续学习Spring MVC


前台输入一个值,提交前台显示


form表单传值,传给后台,get方法

form表单传值,传给后台,post方法

自动生成的映射,包含以下六种数据库接口函数

int deleteByPrimaryKey(Integer id);

int insert(User record);

int insertSelective(User record);

User selectByPrimaryKey(Integer id);

int updateByPrimaryKeySelective(User record);

int updateByPrimaryKey(User record);

对应的sql源码在sqlmap里面

自定义数据库接口

int getCount()

这个函数用来返回数据库对应表里由多条数据,先在sqlmap文件夹下的映射文件userMapper.xml里面写sql代码

<select id="getCount" resultType="java.lang.Integer">
select count(*)from user
</select>

然后到数据库接口文件,dao目录下,userMapper.Java里面,将函数加上


ArrayList<User>selecSelective(User record)

这个函数可以查询符合某一条件的所有纪录,比如我想查询素有name=“lxf"的纪录,只要在record里面record.setName("lxf"),然后用这个函数就能查出并反馈相应集合。

在sqlmap文件夹下的映射文件userMapper.xml里面写sql代码

<select id="selectSelective" resultMap="BaseResultMap" parameterType="com.springmvc.entity.User" >
select
<include refid="Base_Column_List" />
from user
<where>
<if test="id != null" >
id = #{id,jdbcType=INTEGER}
</if>
<if test="name != null" >
AND name = #{name,jdbcType=VARCHAR}

</where>
</select>

userMapper.java里面加上函数

ArrayList<User>selectSelective(User record);

 ArrayList < User > selectLike(User record)

模糊查询,record.setName("lxf"),然后执行函数,返回所有name包括lxf的纪录。

userMapper.xml添加sql代码

<select id="selectLike" resultMap="BaseResultMap" parameterType="com.springmvc.entity.User">
select
<include refid="Base_Column_List"/>
from user
<where>
<if test="id != null and id != ''">
AND id LIKE concat('%',#{id},'%')
</if>
<if test="name != null and name != ''">
AND name LIKE concat('%',#{name},'%')
</if>
</where>
</select>


userMapper.java里面,加上函数

ArrayList<User>selectLike(User record);

使用,联系前台

现在,我们有了数据库接口,但是实际使用的时候,我们是不会直接使用这个数据库接口的,我们要给他封装一下,加个service层,底层数据库接口是不变的,那么想要满足业务需求,一直在变的就是service层的函数

service包新建UserService.java,跟userMapper.java里面加@Repository来表明他是数据库接口的身份一样,这个文件写入@Service表明服务层身份,然后自动注入mapper,添加函数。


现在可以在controller文件里面使用service里面的函数来操作数据库


困难

收获

条件查询和模糊查询函数

数据库操作


明日计划

编写REST接口


返回列表 返回列表
评论

    分享到