1.今天我们来温习一下基于spring+springMvc+Mybatis+前端框架Bootstrap的项目增删改查;
2.首先,我们整合Mybatis
先下载插件在pom.xml中
org.mybatis mybatis 3.4.5 mysql mysql-connector-java 5.1.43
然后再com.mchange c3p0 0.9.5.1
Ssmmybatis(#这是项目名) src/main/java **/*.xml
然后写Person.class实体类
public class person{
private int pid;
private String pname;
private String page;
public Person(int pid, String pname, String page) { this.pid = pid; this.pname = pname; this.page = page; } public Person() { } @Override public String toString() { return "Person{" + "pid=" + pid + ", pname='" + pname + '\'' + ", page='" + page + '\'' + '}'; } public int getPid() { return pid; } public void setPid(int pid) { this.pid = pid; } public String getPname() { return pname; } public void setPname(String pname) { this.pname = pname; } public String getPage() { return page; } public void setPage(String page) { this.page = page; }
}
如下是personDao
public interface personDao{
public Person getPersonById(int pid); public ListgetPersons(); public void deletePersonById(int pid); public void updateperson(Person person); public void addperson(Person person);
}
接下来就是Person.xml
xml version="1.0" encoding="UTF-8" ?> mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
select * from person where pid=#{pid}
select * from person
delete from person where pid=#{pid}
update person set pname=#{pid},page=#{page} where pid=#{pid}
insert into person(pname,page) values(#{pname},#{page})
接下来写resources下的配置文件mybatis-config.xml,
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
最后,在目录src下写个测试类测试一下,如下:
package com.zking.test; import com.zking.dao.PersonDao; 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; public class SsmTest {
public void test1(){
try {
SqlSessionFactory sqlsessionfactory=new SqlSessionFactoryBuilder().bulid(Resources.getResourceAsStream("mybatis-config.xml"))
SqlSession sqlSession = sqlSessionFactory.openSession(); PersonDao personDao=sqlSession.getMapper(PersonDao.class); /* personDao.deletePersonById(17);*/ System.out.println(personDao.getPersons().size()); sqlSession.commit(); sqlSession.close(); } catch (IOException e) { e.printStackTrace(); } }
}
}
测试一下Mybatis,测试成功,则开始配置spring,再配置springMVC;
这时pom.xml里又要添加插件了,
org.springframework spring-webmvc 4.3.3.RELEASE
org.springframework spring-jdbc 4.3.10.RELEASE
然后再resource目录下,新建一个applicationContext.xml文件org.mybatis mybatis-spring 1.3.1
xml version="1.0" encoding="UTF-8"?>xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" 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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> name="dataSource" ref="dataSource"> name="configLocation" value="classpath:mybatis-config.xml"> name="mapperLocations" value="classpath:com/zking/entity/*.xml"> id="personDao" class="org.mybatis.spring.mapper.MapperFactoryBean"> name="sqlSessionFactory" ref="sqlSessionFactory"> name="mapperInterface" value="com.zking.dao.PersonDao">
接下来是数据库文件
db.properties
uname=root upass=root url=jdbc:mysql://localhost:3306/mysql driver_Class=com.mysql.jdbc.Driver initPoolSize=5 maxPoolSize=20然后在新建一个测试类
TestSpring
package com.zking.test; import com.zking.dao.PersonDao; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestSpring { @Test public void test(){ ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicaionContext.xml"); PersonDao personDao= (PersonDao) applicationContext.getBean("PersonDao"); System.out.println(personDao.getPersons().size()); } }最后配置springMVC.xml