标签:spring findall select load 生成 ext oid mys oca
一、配置properties属性文件读取数据源信息
1、db.properties的配置属性文件
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test?characterEncoding=utf-8
name=root
pass=0000
2、spring核心配置文件的编写
<!-- 使用属性文件配置数据源 -->
<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="classpath:db.properties"/> </bean> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${driver}"></property> <property name="url" value="${url}"></property> <property name="username" value="${name}"></property> <property name="password" value="${pass}"></property> </bean> <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="configLocation" value="classpath:configuration.xml"/> <property name="mapperLocations"> <list> <value>com/entity/*.xml</value> </list> </property> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.mapper"/> </bean> <bean id="userService" class="com.service.UserService"> <property name="userMapper" ref="userMapper"/> </bean>
二、通过JNDI读取数据源信息的配置
1、在META-INF目录下配置context.xml文件
<?xml version="1.0" encoding="UTF-8"?> <Context> <Resource name="jdbc/news" auth="Container" type="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWait="10000" username="root" password="0000" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf-8" /> </Context>
2、spring核心配置文件的编写
<!-- 通过JNDI配置DataSource --> <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> <!--通过jndiName指定引用的JNDI数据源名称 --> <property name="jndiName"> <value>java:comp/env/abc</value> </property> </bean> <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="configLocation" value="classpath:configuration.xml"/> <property name="mapperLocations"> <list> <value>com/entity/*.xml</value> </list> </property> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.mapper"/> </bean> <bean id="userService" class="com.service.UserService"> <property name="userMapper" ref="userMapper"/> </bean>
三、bean的单例和原型的两种设置
<!-- 可以在bean的配置中设置 --> <bean id="user" class="com.entity.User" scope="singleton"></bean> <bean id="user" class="com.entity.User" scope="prototype"></bean>
//注解的方式设置bean的作用域 @Component //@Scope("singleton") @Scope("prototype") public class User { private Integer uid; private String uname; }
<!-- 使用注解spring的配置文件中需设置自动扫描 --> <context:component-scan base-package="com.entity"/>
四、spring中多种方式的自动装配
1、在类中使用注解进行装配
@Service public class UserService { @Autowired //or @Resource private UserMapper userMapper; public void setUserMapper(UserMapper userMapper) { this.userMapper = userMapper; } public List<User> findAll(){ return userMapper.findAll(); } }
2、在配置文件中设置autowire属性进行注入
<!-- 在配置文件中设置autowire属性进行注入 --> <bean id="userService" class="com.service.UserService" autowire="byName"> <!-- <property name="userMapper" ref="userMapper"/> --> </bean>
也可以设置全局的装配方式,但要注意bean的作用域,需要进行spring核心文件的拆分
<?xml version="1.0" encoding="UTF-8"?> <beans default-autowire="byName" xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" 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.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd"> ......省略 <!-- 在配置文件中设置autowire属性进行注入 --> <bean id="userService" class="com.service.UserService"> <!-- <property name="userMapper" ref="userMapper"/> --> </bean> </beans>
五、spring核心文件的拆分
<!-- spring文件拆分后可以用import标签导入 --> <import resource="classpath:applicationContext.xml"/>
六、一键生成的工具使用
SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder(); SqlSessionFactory f=builder.build(Test.class.getClassLoader().getResourceAsStream("configuration.xml")); SqlSession s=f.openSession(); StudentMapper m=s.getMapper(StudentMapper.class); //m.insert();//如果实体中的属性值为null,则会把null插入到表中 //m.insertSelective();//如果实体中的属性值为null,则不会把null插入到表中,而是走数据库的null值,效率高 //m.deleteByPrimaryKey(id);//普通删除 //m.updateByPrimaryKey(record);//如果实体中的属性值是null,则会把表中该列设置成null值 //m.updateByPrimaryKeySelective(record);//如果实体中的属性值是null,则不会动表中该列,效率高 //m.selectByPrimaryKey(id);//普通的查询 StudentExample e=new StudentExample(); Criteria c=e.createCriteria(); c.andAgeGreaterThanOrEqualTo(20); c.andNameLike("%李%3%"); List<Student> list=m.selectByExample(e); for (Student student : list) { System.out.println(student); }
标签:spring findall select load 生成 ext oid mys oca
原文地址:https://www.cnblogs.com/newbest/p/9206085.html