mybatis实现增删改查xml配置和后端Java编程完整教程
生活随笔
收集整理的這篇文章主要介紹了
mybatis实现增删改查xml配置和后端Java编程完整教程
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
mybatis實(shí)現(xiàn)增刪改查xml配置和后端編程教程
SqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration><environments default="development"><environment id="development"><!-- 使用jdbc事務(wù)管理 --><transactionManager type="JDBC"/><!-- 數(shù)據(jù)庫(kù)連接池 --><dataSource type="POOLED"><property name="driver" value="com.mysql.jdbc.Driver"/><property name="url" value="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8"/><property name="username" value="root"/><property name="password" value="011220"/></dataSource></environment></environments><mappers><mapper resource="sqlmap/user.xml"/></mappers></configuration>user.xml
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="test"><!-- 根據(jù)用戶id查詢用戶信息 --><select id="findUserById" parameterType="int" resultType="cn.nwtxxb.mybatis.po.User">select * from user where id = #{id}</select><!-- 根據(jù)用戶名查詢用戶信息 --><select id="findUserByUserName" parameterType="java.lang.String" resultType="cn.nwtxxb.mybatis.po.User">select * from user where username like '%${value}%'</select><!-- 添加用戶 --><insert id="insertUser" parameterType="cn.nwtxxb.mybatis.po.User"><!-- selectKey將主鍵返回 --><selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">select LAST_INSERT_ID()</selectKey>insert into user (username,birthday,sex,address) values (#{username},#{birthday},#{sex},#{address})</insert><!-- 刪除用戶 --><delete id="deleteUserById" parameterType="int">delete from user where id=#{id}</delete></mapper>User.java
package cn.nwtxxb.mybatis.po;import java.util.Date;public class User {private int id;private String username;private String sex;private Date birthday;private String address;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}public Date getBirthday() {return birthday;}public void setBirthday(Date birthday) {this.birthday = birthday;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}@Overridepublic String toString() {return "User [id=" + id + ", username=" + username + ", sex=" + sex + ", birthday=" + birthday + ", address="+ address + "]";}}測(cè)試類代碼:
package cn.nwtxxb.mybatis.test; import java.io.InputStream; import java.util.Date; import java.util.List; 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.Before; import org.junit.Test; import cn.nwtxxb.mybatis.po.User; public class Mybatis_first {//會(huì)話工廠private SqlSessionFactory sqlSessionFactory;@Beforepublic void createSqlSessionFactory() throws Exception{//配置文件String resource = "SqlMapConfig.xml";InputStream inputStream = Resources.getResourceAsStream(resource);//使用SqlSessionFactoryBuilder從xml配置文件中創(chuàng)建SqlSessionFactorysqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);}//根據(jù)id查詢用戶信息@Testpublic void testFindUserById(){//數(shù)據(jù)庫(kù)會(huì)話實(shí)例SqlSession sqlSession = null;try {//創(chuàng)建數(shù)據(jù)庫(kù)會(huì)話實(shí)例sqlSessionsqlSession = sqlSessionFactory.openSession();//查詢單個(gè)記錄,根據(jù)用戶id查詢用戶信息User user = sqlSession.selectOne("test.findUserById",10);//輸出用戶信息System.out.println(user);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{if(sqlSession!=null){sqlSession.close();}}}//根據(jù)用戶名查詢用戶信息@Testpublic void testFindUserByUsername(){//數(shù)據(jù)庫(kù)會(huì)話實(shí)例SqlSession sqlSession = null;try {//創(chuàng)建數(shù)據(jù)庫(kù)會(huì)話實(shí)例sqlSessionsqlSession = sqlSessionFactory.openSession();//根據(jù)用戶名中包含"張"的條件查詢用戶信息List<User> list = sqlSession.selectList("test.findUserByUserName","張");System.out.println(list.size());} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{if(sqlSession!=null){sqlSession.close();}}}//添加用戶信息@Testpublic void testInsert(){//數(shù)據(jù)庫(kù)會(huì)話實(shí)例SqlSession sqlSession = null;try {//創(chuàng)建數(shù)據(jù)庫(kù)會(huì)話實(shí)例sqlSessionsqlSession = sqlSessionFactory.openSession();//添加用戶信息User user = new User();user.setUsername("張磊");user.setSex("1");user.setAddress("襄城");user.setBirthday(new Date());sqlSession.insert("test.insertUser",user);//提交事務(wù)sqlSession.commit();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{if(sqlSession!=null){sqlSession.close();}}}//刪除用戶@Testpublic void testDelete(){//數(shù)據(jù)庫(kù)會(huì)話實(shí)例SqlSession sqlSession = null;try {//創(chuàng)建數(shù)據(jù)庫(kù)會(huì)話實(shí)例sqlSessionsqlSession = sqlSessionFactory.openSession();sqlSession.delete("test.deleteUserById",26);sqlSession.commit();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{if(sqlSession!=null){sqlSession.close();}}} }login4j.properties
# Global logging configuration log4j.rootLogger=DEBUG,stdout # Console output... log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n總結(jié)
以上是生活随笔為你收集整理的mybatis实现增删改查xml配置和后端Java编程完整教程的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mybatis完整增删改查入门实例
- 下一篇: MyBatis DAO层开发——Mapp