mybatis增删改查
maybatis-config.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | <?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(); } }
【推荐】中国电信天翼云云端翼购节,2核2G云服务器一口价38元/年
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步