SSM框架学习笔记——mybatis及一些配置

1.在applicationContext.xml中添加mybatis配置
在这里插入图片描述
在这里插入图片描述
然后建立UserDao.xml
1、applicationContext.xml配置了service包下面的类中create开头的方法都添加了事务机制,运行–结果数据库无数据添加
2、注释掉xml中的create*的配置,看下结果,插入一条数据
3、在service的create方法或者serviceImpl类上加上注解,如下,再运行,发现无数据添加成功
下面就是建类了
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zq.model.User;
import com.zq.service.UserService;

@RequestMapping("/user")
@Controller
public class UserController {

@Autowired
private UserService userService;


@ResponseBody
@RequestMapping("/create.do")
public boolean create(User user){
	try{
		userService.create(user);
	}catch(Exception e){
		System.out.println(e.getMessage());	
		return false;
	}
	return true;
}

@ResponseBody
@RequestMapping("/list.do")
public List<User> list(User user){
	return userService.list(user);
}


@RequestMapping("/toLogin.do")
public String toLogin(){
	return "login";
}

}
还有UserDao.xml的SQL语句:

	<where><!-- 这种写法会自动去掉第一个and -->
		<if test="username!=null and username!=''">
			and username = #{username}
		</if>
		<if test="pwd!=null and pwd!=''">
			and pwd = #{pwd}
		</if>
		<!-- id与字符窜判断区别 -->
		<if test="id !=null">
		and id = #{id}
		</if>
		<if test="realname!=null and realname!=''">
		and realname like concat('%',#{realname},'%')<!-- '%'#{realname}'%' --> 
		</if>
	</where>
	 </sql>
	 <!-- 查询列表 -->
<select id="list" parameterType="user" resultType="user">
	select * from user 
	<include refid="sqlWhere" />
</select>

<!-- id不需要,自增 -->
<insert id="create" parameterType="user">
insert into user(username,pwd,realname)
values(#{username},#{pwd},#{realname})
</insert>
update user username = #{username} pwd = #{pwd} realname = #{realname} where id = #{id} delete from user where id = #{id} 连接数据库后,在网页测试效果。
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值