mybatis-java1234一
一,简单增删改代码实现
package com.mybatis.mapper; import java.util.List; import com.mybatis.model.Student; public interface StudentMapper { public int add(Student student); public int update(Student student); public int delete(Integer id); public Student findById(Integer id); public List<Student> findAll(); }
<?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="com.mybatis.mapper.StudentMapper"> <resultMap type="student" id="studentResult"> <id property="id" column="id"/> <result property="name" column="name"/> <result property="age" column="age"/> </resultMap> <insert id="add" parameterType="student" > insert into t_student values(null,#{name},#{age}) </insert> <update id="update" parameterType="student"> update t_student set name=#{name},age=#{age} where id=#{id} </update> <delete id="delete" parameterType="Integer"> delete from t_student where id=#{id} </delete> <select id="findById" parameterType="Integer" resultType="student"> select * from t_student where id=#{id} </select> <select id="findAll" resultMap="studentResult"> select * from t_student </select> </mapper>
package com.mybatis.model; public class Student { private Integer id; private String name; private Integer age; public Student() { super(); // TODO Auto-generated constructor stub } public Student(String name, Integer age) { super(); this.name = name; this.age = age; } public Student(int id, String name, int age) { super(); this.id = id; this.name = name; this.age = age; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } /* (non-Javadoc) * @see java.lang.Object#toString() */ @Override public String toString() { return super.toString(); } }
package com.mybatis.testjdbc01; import org.apache.ibatis.session.SqlSession; import org.apache.log4j.Logger; import com.mybatis.mapper.StudentMapper; import com.mybatis.model.Student; import com.mybatsi.util.SessionFactoryUtil; public class TestJdbcMybatis01 { private static Logger logger = Logger.getLogger(TestJdbcMybatis01.class); public static void main(String[] args) { SqlSession sqlSession = SessionFactoryUtil.openSession(); StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class); Student student=new Student("lis",11); int result=studentMapper.add(student); sqlSession.commit(); if(result>0){ System.out.println("添加成功!"); logger.info("添加成功"); } } }
package com.mybatis.testjdbc01; import static org.junit.Assert.fail; import java.util.List; import org.apache.ibatis.session.SqlSession; import org.apache.log4j.Logger; import org.junit.After; import org.junit.Before; import org.junit.Test; import com.mybatis.mapper.StudentMapper; import com.mybatis.model.Student; import com.mybatsi.util.SessionFactoryUtil; public class TestJdbcMybatis02 { private static Logger logger = Logger.getLogger(TestJdbcMybatis01.class); private SqlSession sqlSession = null; private StudentMapper studentMapper = null; /** * 测试方法调用前执行 * @throws Exception */ @Before public void setUp() throws Exception { sqlSession = SessionFactoryUtil.openSession(); studentMapper = sqlSession.getMapper(StudentMapper.class); } /** * 测试方法后调用 * @throws Exception */ @After public void tearDown() throws Exception { sqlSession.close(); } /** * 测试方法 */ @Test public void test() { logger.info("添加学生"); Student student=new Student("wang5",12); int result=studentMapper.add(student); sqlSession.commit(); } @Test public void testUpdate() { logger.info("修改学生"); Student student=new Student(4,"wang2",13); studentMapper.update(student); sqlSession.commit(); } @Test public void testDelete(){ logger.info("删除学生"); studentMapper.delete(8); sqlSession.commit(); } @Test public void testFindById(){ logger.info("根据id查找学生"); Student student = studentMapper.findById(1); sqlSession.commit(); } @Test public void findAll(){ logger.info("查找所有学生"); List<Student> student = studentMapper.findAll(); for(Student stu : student){ System.out.println(stu.getName()); } sqlSession.commit(); } }
package com.mybatsi.util; import java.io.InputStream; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; public class SessionFactoryUtil { private static SqlSessionFactory sqlSessionFactory; public static SqlSessionFactory getSessionFactiory(){ if(sqlSessionFactory == null){ InputStream inputStream = null; try { inputStream = Resources.getResourceAsStream("mybatis-config.xml"); sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } catch (Exception e) { e.printStackTrace(); } } return sqlSessionFactory; } public static SqlSession openSession(){ return getSessionFactiory().openSession(); } }
log4j.rootLogger=info,appender1,appender2 log4j.appender.appender1=org.apache.log4j.ConsoleAppender log4j.appender.appender2=org.apache.log4j.FileAppender log4j.appender.appender2.File=D:/logFile.txt log4j.appender.appender1.layout=org.apache.log4j.TTCCLayout log4j.appender.appender2.layout=org.apache.log4j.TTCCLayout
<?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> <properties resource="jdbc.properties"/> <typeAliases> <typeAlias alias="student" type="com.mybatis.model.Student"/> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </dataSource> </environment> </environments> <mappers> <mapper resource="com/mybatis/mapper/StudentMapper.xml" /> <!-- <mapper class="com.mybatis.mapper.StudentMapper"/> --> </mappers> </configuration>