发表于: 2017-11-09 21:31:30
2 831
今天学习的内容
今天学习mybatis与spring集成,很多的项目中,通常会用 spring 这个粘合剂来管理 datasource 等.充分利用 spring 基于接口的编程,以及aop,ioc 带来的方便.用 spring 来管理 mybatis 与管理 hibernate 有很多类似的地方.以下是我按照示例敲的代码:
BmbTest.java
package model;
public class BmbTest {
private int id;
private String name;
private String sex;
private int age;
private String adress;
private String study;
public int getId() { return id; }
public void setId(int id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getSex() { return sex; }
public void setSex(String sex) { this.sex = sex; }
public int getAge() { return age; }
public void setAge(int age) { this.age = age; }
public String getAdress() { return adress; }
public void setAdress(String adress) { this.adress = adress; }
public String getStudy() { return study; }
public void setStudy(String study) { this.study = study; }
public void bmbTestMod(int id,String name,String sex,int age,String adress, String study){
this.id=id;
this.name=name;
this.sex=sex;
this.age=age;
this.adress=adress;
this.study=study;
}
@Override
public String toString(){
return "id:"+id+"\tname:"+name+"\tsex:"+sex+"\tage:"+age+"\tadress:"+adress+"\tstudy:"+study;
}
}
BmbTestMapper.java
package mapper;
import model.BmbTest;
import java.util.List;
public interface BmbTestMapper {
public int bmbTestInsert(BmbTest bmbTest);
public int bmbTestDelete(int id);
public int bmbTestUpdate(BmbTest bmbTest);
public List<BmbTest> listAll();
public BmbTest bmbTestSelect(BmbTest bmbTest);
}
BmbTestService.java
package service;
import mapper.BmbTestMapper;
import model.BmbTest;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
public class BmbTestService {
private static Logger logger= Logger.getLogger(BmbTestService.class);
public static void main(String[] args){
/*
* ApplicationContext生成工厂对象。加载bean配置文件
* 利用框架提供的 ClassPathXmlApplicationContext API 去生成工厂 bean。
* ClassPathXmlApplicationContext 负责生成和初始化所有的对象
*/
ApplicationContext ctx=new ClassPathXmlApplicationContext("ApplicationContext-c3p0.xml");
//getBean() 方法得到所需要的bean
BmbTestMapper bmbTestMapper=(BmbTestMapper) ctx.getBean("BmbTestMapper");
try {
logger.info("测试插入");
BmbTest bmbTest=new BmbTest();
bmbTest.bmbTestMod(0,"陆压","男",18,"鱼鲮岛","斩仙飞刀");
//执行SQL语句
bmbTestMapper.bmbTestInsert(bmbTest);
}catch (Exception e){
e.printStackTrace();
logger.error("插入出错:"+e.getMessage());
}
try {
logger.info("测试删除");
bmbTestMapper.bmbTestDelete(1);
}catch (Exception e){
e.printStackTrace();
logger.error("删除出错:"+e.getMessage());
}
try {
logger.info("测试更改");
BmbTest bmbTest2=new BmbTest();
bmbTest2.setId(2);
bmbTest2.setStudy("吸星大法");
bmbTestMapper.bmbTestUpdate(bmbTest2);
}catch (Exception e){
e.printStackTrace();
logger.error("更改出错:"+e.getMessage());
}
try {
logger.info("测试查找");
BmbTest bmbTest3=new BmbTest();
bmbTest3.setId(3);
bmbTestMapper.bmbTestSelect(bmbTest3);
logger.info(bmbTestMapper.bmbTestSelect(bmbTest3));
}catch (Exception e){
e.printStackTrace();
logger.error("查找出错:"+e.getMessage());
}
try {
logger.info("测试遍历");
List<BmbTest> listAll=bmbTestMapper.listAll();
logger.info(listAll.toString()+"\n\n");
}catch (Exception e){
e.printStackTrace();
logger.error("遍历出错:"+e.getMessage());
}
}
}
applicationContest.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
<property name="driverClassName"
value="com.mysql.jdbc.Driver"/>
<property name="url"
value="jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8&useSSL=false" />
<property name="username" value="root" />
<property name="password" value="123456" />
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value=""/>
<property name="mapperLocations" value="classpath:BmbTestMapper.xml"/>
</bean>
<bean id="bmbTestMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="MapperInterface" value="mapper.BmbTestMapper" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
</beans>
BmbTestMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- 不写会报错 -->
<!DOCTYPE mapper PUBLIC "-//mybatis.org/DTD Mapper 3.0" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="mapper.BmbTestMapper">
<insert id = "bmbTestInsert" parameterType="model.BmbTest" flushCache="true" >
insert into bmbtest (name,sex,age,adress,study)
VALUE (#{name},#{sex},#{age},#{adress},#{study})
</insert>
<update id = "bmbTestUpdate" parameterType="model.BmbTest">
UPDATE bmbtest set name = #{name} where id = #{id}
</update>
<delete id = "bmbTestDelete" parameterType="model.BmbTest">
delete from bmbtest where id = #{id}
</delete>
<select id = "bmbTestSelect" parameterType="int" resultType="model.BmbTest">
SELECT name FROM bmbtest where id = #{id}
</select>
<select id = "listAll" resultType="model.BmbTest">
SELECT * FROM bmbtest
</select>
</mapper>
遇到的问题
今天其实写了两个项目,但是第一个项目参考的实例细节上有很多问题,所以重写了一份,导致时间上浪费了很多,以后找资料的时候一定要注意避免这类事发生.
今天的收获
今天学习了Mybatis和Spring集成的概念,按照实例写了一个项目.
明天的计划
明天使用该项目完成对表的增删改查.
任务进度:任务1步骤17
任务开始时间:10月28日
任务结束时间:11月10日
可能要延期到11月13号
评论