标签:数据库 编程 结果 单词 href 设计 xmlns resource 业务
整合基础说明
好了不废话了,直接开始入门程序吧,jar包下载,mysql数据库语句下载
首先看看创建目录
然后开始进行编码整合了。
1.在po包中创建一个Items.java
package cn.my.ssm.po; import java.util.Date; public class Items { private Integer id; private String name; private Float price; private String pic; private Date createtime; private String detail; 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 == null ? null : name.trim(); } public Float getPrice() { return price; } public void setPrice(Float price) { this.price = price; } public String getPic() { return pic; } public void setPic(String pic) { this.pic = pic == null ? null : pic.trim(); } public Date getCreatetime() { return createtime; } public void setCreatetime(Date createtime) { this.createtime = createtime; } public String getDetail() { return detail; } public void setDetail(String detail) { this.detail = detail == null ? null : detail.trim(); } @Override public String toString() { return "Items [id=" + id + ", name=" + name + ", price=" + price + ", pic=" + pic + ", createtime=" + createtime + ", detail=" + detail + "]"; } }
2.在mapper包中创建一个ItemMapper.java
package cn.my.ssm.mapper; import cn.my.ssm.po.Items; public interface ItemsMapper { List<Items> selectFind(Items items); }
3.在sqlmap包中创建一个Items.xml映射文件,mybatis的映射配置我就不多说了,不太清楚的可以看看前面章节mybatis入门程序
<?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"> <!-- 为了分辨多个mybatis的映射文件同名问题所以使用接口包名作为空间 --> <mapper namespace="cn.my.ssm.mapper.ItemsMapper"> <sql id="items_where"> <if test="items != null"> <if test="items.name != null and items.name != ‘‘"> and name like ‘%${items.name}%‘ </if> </if> </sql> <select id="selectFind" parameterType="cn.my.ssm.po.ItemsQueryVo" resultType="cn.my.ssm.po.Items"> SELECT * FROM items <where> <include refid="items_where"></include> </where> </select> </mapper>
4.在mybatis包中创建一个SqlMapConfig.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> <mappers> <mapper resource="sqlmap/Items.xml"/> </mappers> </configuration>
5.配置数据源我记不住单词,都是直接找jar,然后双击点开找到该类中的属性
5.1.配置数据源
5.2配置sqlsessionfactory工厂,同样的操作
5.3.配置映射mapper接口
5.4spring配置
<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <!-- 读取数据源连接配置文件 --> <context:property-placeholder location="classpath:db.properties"/> <!-- 1.配置dataSource数据源,我用的是commons-dbcp-1.2.2.jar连接 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${jdbc.driver}"></property> <property name="url" value="${jdbc.url}"></property> <property name="username" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> <property name="maxActive" value="30"></property> <property name="maxIdle" value="5"></property> </bean> <!-- 2.配置工厂SqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"></property> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 3.配置映射mapper接口 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 这里我们使用组件扫描 --> <property name="basePackage" value="cn.my.ssm.mapper"></property> <!-- 注意这里一定要使用sqlSessionFactoryBeanName不能使用sqlSessionFactory属性, 要不然读取不到数据源的db.properties文件 --> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean> </beans>
首先看看spring和mybatis是否整合成功
package cn.my.ssm.test; import java.util.List; import org.junit.Before; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.my.ssm.mapper.ItemsMapper; import cn.my.ssm.po.Items; public class TestItem { ApplicationContext context; @Before public void setUp(){ context = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-dao.xml"); } @Test public void testFindById() throws Exception{ ItemsMapper userMapper = (ItemsMapper) context.getBean("itemsMapper"); List<Items> list = userMapper.selectFind(null); System.out.println(list); } }
运行结果
[Items [id=1, name=台式机, price=3000.0, pic=null, createtime=Tue Oct 03 23:22:36 CST 2017, detail=该电脑质量非常好!!!!111], Items [id=2, name=笔记本, price=7000.0, pic=null, createtime=Tue Oct 03 23:23:06 CST 2017, detail=笔记本性能好,质量好!!!!!], Items [id=3, name=背包, price=1200.0, pic=null, createtime=Tue Oct 03 23:23:21 CST 2017, detail=名牌背包,容量大质量好!!!!]]
以上就动态代理整合, 如果大家有疑问的可以加群号581591235或者留言。或者加我微信群,dcc939705214加时备注是从哪里看到的
标签:数据库 编程 结果 单词 href 设计 xmlns resource 业务
原文地址:http://www.cnblogs.com/duchaochen/p/7625464.html