1.今天我们来温习一下基于spring+springMvc+Mybatis+前端框架Bootstrap的项目增删改查;
2.首先,我们整合Mybatis
先下载插件在pom.xml中
<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.5</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.43</version> </dependency>
<dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.1</version> </dependency>然后再<build>中配置
<build> <finalName>Ssmmybatis(#这是项目名)</finalName> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> </resources> </build>
然后写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 List<Person> getPersons(); public void deletePersonById(int pid); public void updateperson(Person person); public void addperson(Person person);
}
接下来就是Person.xml
<?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.zking.dao.PersonDao">
<!--一个select对应一个dao方法-->
<!--根据id获得对象-->
<select id="getPersonById" parametertype="int" resulttype="com.zking.entity.person">
select * from person where pid=#{pid}
</select>
<!--获取所有对象-->
<select id="getpersons" resulttype="com.zking.entity.person">
select * from person
</select>
<!--删除没有返回对象-->
<select id="deletePersonById" parametertype="int" >
delete from person where pid=#{pid}
</select>
<!--修改-->
<select id="updateperson" parametertype="com.zking.entity.person">
update person set pname=#{pid},page=#{page} where pid=#{pid}
</select>
<!--添加-->
<select id="addperson" parametertype="com.zking.entity.person">
insert into person(pname,page) values(#{pname},#{page})
</select>
</mapper>
接下来写resources下的配置文件mybatis-config.xml,
<?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>
<!--设置别名-->
<typeAliases>
<package name="com.zking"></package>
</typeAliases>
<!--以下这段在配置Mybatis用到,ssm时删除-->
<!--<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://119.23.225.13:3306/test"/>
<property name="username" value="root"/>
<property name="password" value="Zkingadmin,."/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/zking/entity/Person.xml"/>
</mappers>-->
</configuration>
最后,在目录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里又要添加插件了,
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.3.3.RELEASE</version> </dependency>
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.3.10.RELEASE</version> </dependency>
<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.1</version> </dependency>然后再resource目录下,新建一个applicationContext.xml文件
<?xml version="1.0" encoding="UTF-8"?> <beans 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"><context:property-placeholder location="classpath:db.properties"></ context:property-placeholder>
<bean id="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${uname}"></property>
<property name="password" value="${upass}"></property>
<property name="jdbcUrl" value="${url}"></property>
<property name="driverClass" value="${driver_Class}"></property>
<property name="initialPoolSize" value="${initPoolSize}"></property>
<property name="maxPoolSize" value="${maxPoolSize}"></property>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="configLocation" value="classpath:mybatis-config.xml"></property> <property name="mapperLocations" value="classpath:com/zking/entity/*.xml"></property> </bean> <bean id="personDao" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> <property name="mapperInterface" value="com.zking.dao.PersonDao"></property> </bean></beans>
接下来是数据库文件
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