发表于: 2017-06-29 19:21:07
1 1089
今天完成的事情:
整合spring+mybatis
准备一个pojo和一个映射类
package
com.how2java.mapper;
import
java.util.List;
import
com.how2java.pojo.Category;
public
interface
CategoryMapper {
public
int
add(Category category);
public
void
delete(int
id);
public
Category get(int
id);
public
int
update(Category category);
public
List<Category> list();
public
int
count();
}
配置文件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.how2java.mapper.CategoryMapper">
<insert
id="add"
parameterType="Category"
>
insert into category_ ( name ) values (#{name})
</insert>
<delete
id="delete"
parameterType="Category"
>
delete from category_ where id= #{id}
</delete>
<select
id="get"
parameterType="_int"
resultType="Category">
select * from category_ where id= #{id}
</select>
<update
id="update"
parameterType="Category"
>
update category_ set name=#{name} where id=#{id}
</update>
<select
id="list"
resultType="Category">
select * from category_
</select>
</mapper>
在src下的主配置文件xml
<?xml
version="1.0"
encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-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
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:annotation-config
/>
<bean
id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property
name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property
name="url">
<value>jdbc:mysql://localhost:3306/how2java?characterEncoding=UTF-8</value>
</property>
<property
name="username">
<value>root</value>
</property>
<property
name="password">
<value>admin</value>
</property>
</bean>
<bean
id="sqlSession"
class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 这个在之前mybatis中由对象映射出来的xml文件构建sqlsession。现在交由spring处理 >
<property
name="typeAliasesPackage"
value="com.how2java.pojo"
/>
<property
name="dataSource"
ref="dataSource"/>
<property
name="mapperLocations"
value="classpath:com/how2java/mapper/*.xml"/>
</bean>
<bean
class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property
name="basePackage"
value="com.how2java.mapper"/>
</bean>
</beans>
使用Spring注解方式测试,拿到注入的CategoryMapper对象,当调用add方法的时候,会自动去找Category.xml里id="add"的sql语句。
package
com.java.test;
import
java.util.List;
import
org.junit.Test;
import
org.junit.runner.RunWith;
import
org.springframework.beans.factory.annotation.Autowired;
import
org.springframework.test.context.ContextConfiguration;
import
org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import
com.how2java.mapper.CategoryMapper;
import
com.how2java.pojo.Category;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public
class
MybatisTest {
@Autowired
private
CategoryMapper categoryMapper;
@Test
public
void
testAdd() {
Category category = new
Category();
category.setName("new Category");
categoryMapper.add(category);
}
@Test
public
void
testList() {
System.out.println(categoryMapper);
List<Category> cs=categoryMapper.list();
for
(Category c : cs) {
System.out.println(c.getName());
}
}
}
明天的计划:学习一下tomcat,tomcat很重要的说,但是自己看不懂,配置好了,就是无法部署。顺便买个服务器把程序上传上去
遇到的问题:恩,今天在整合玩spring和mybatis过后,有一个东西,发现要是tomcat。这个东西日后ssm都需要用到的,弄不懂估计很难继续开展其他的项目。
1.tomcat配置好环境变量,但是用他来打开html文件一直无法显示。应该是没有正确部署,在网上找了很多教程和一些排除的方法都没发现异常在哪里。
收获:初步简单的整合了spring和mybatis,还有接触了一下springMVC和tomcat。知道了tomcat在日后web项目中的重要性,日后ssm需要用到他的地方非常的多。但是没有搞懂这一个tomcat为什么无法正常显示。弄个了很久。端口号更改过后,发现还是有空白的东西,什么都不显示。明天还是要多多研究一下tomcat的部署情况
评论