mybatis增删改查
Posted 春眠不觉笑
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mybatis增删改查相关的知识,希望对你有一定的参考价值。
maybatis-config.xml
<?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> <!--节点是有顺序的--> <!--先是jdbc,log4j--> <properties resource="jdbc.properties"></properties> <settings> <setting name="logImpl" value="LOG4J"/> </settings> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </dataSource> </environment> </environments> <mappers> <mapper resource="mapper/IUserDao.xml"/> </mappers> </configuration>
mapper / IUserDao
<?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="cn.kitty.dao.IUserDao"> <select id="findAll" resultType="cn.kitty.bean.User" > select * from news_users </select> <insert id="addUser" parameterType="cn.kitty.bean.User" > INSERT INTO news_users(uid,uname,upwd) VALUES(#{uid},#{uname},#{upwd}) </insert> <update id="updateUser" parameterType="cn.kitty.bean.User"> update news_users set uname=#{uname},upwd=#{upwd} where uid=#{uid} </update> </mapper>
jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql:///school jdbc.username=root jdbc.password=
log4j.properties
log4j.appender.console=org.apache.log4j.ConsoleAppender log4j.appender.console.Target=System.out log4j.appender.console.layout=org.apache.log4j.PatternLayout log4j.appender.console.layout.ConversionPattern=[%-5p] %c %L %m %n log4j.rootLogger=trace,console
userTest
package UserTest; import cn.kitty.bean.User; import cn.kitty.dao.IUserDao; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.junit.Test; import java.io.IOException; import java.io.InputStream; import java.util.List; import static org.apache.ibatis.io.Resources.*; public class UserTest { @Test public void findAll(){//查询 //1.根据大配置,装载 数据库的连接信息 SqlSession session=null; //2.将硬盘上的文件和流绑定 try { InputStream is = getResourceAsStream("mybaits1-config.xml"); SqlSessionFactory factory =new SqlSessionFactoryBuilder().build(is); session = factory.openSession(); List<User> list =session.selectList("findAll"); for ( User user :list) { System.out.println(user); } } catch (IOException e) { e.printStackTrace(); } } @Test public void add(){//新增 SqlSession session=null; try { InputStream is =getResourceAsStream("mybaits1-config.xml"); SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(is); session= factory.openSession(); IUserDao mapper=session.getMapper(IUserDao.class); User user=new User(); user.setUname("崔小梦"); user.setUpwd("123"); mapper.addUser(user); System.out.println("save ok!"); session.commit(); } catch (IOException e) { e.printStackTrace(); session.rollback(); } session.close(); } @Test public void update(){ SqlSession session=null; try { InputStream is=getResourceAsStream("mybaits1-config.xml"); SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(is); session= factory.openSession(); IUserDao mapper=session.getMapper(IUserDao.class); User user=new User(); user.setUname("hehheh"); user.setUpwd("qwertyu"); user.setUid(9); int count= mapper.updateUser(user); System.out.println(count); System.out.println("update ok!"); session.commit(); } catch (IOException e) { e.printStackTrace(); session.rollback(); } session.close(); } }
以上是关于mybatis增删改查的主要内容,如果未能解决你的问题,请参考以下文章