前一篇文章写得是xml文件来配置数据库连接的。但是为了方便,我们实际中采用的是properties文件的方式来配置数据库的。修改properties 文件 会比 修改 xml文件 方便。
做法是:
将经常需要修属性参数值,配置到独立properties文件 ,然后在xml文件引入properties
先给出整个案例的结构图:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAARAAAACrCAIAAAAhElRAAAAbpklEQVR4nO2dbXAUx5nHxx9c+XJVR5WvykdVqmLn7mwc19mHbJzLhii+JGdxl6tzLi5slApVxC8bK1wCOHYMBsWSQERZg7OJJQPByHJs8FqWY2yZBZ2DQSESEBQssIUVthZJrEDSane1kvZlVvvy3IfZne2Z6Z6dkfZldvT8qz/s9vR098z2b7p7dv49HKBQKIV4nvd4PGNjY36/f4oQV7giP+4/1fzB7g2dnc0f7P64/1ThCkKh8q5iA/Nx/6kNnZ2uvjZ+7MyGzk7LkU+QGVQZqeDAXHhnixgAwPHhIISGPrvY0uPeeeTT31a3tD+y2ZGvsiRyWjmL3QUA4LJbOKuzIIXMV06r0WokUW9Xrc0NAF6Ho7amz1vq6kjk6Wuu6hooWfGFBaa/Y4v06+YNnZ0df730+skDbzj/o/XAt9bXvlr9k4UBkwWDFW8eYNztVS21VS3tvUK7aaG05t6u2qqWZkeAnoGwlxhsbnqyPAPjbhfqnFagu4asoexrLpkRmJb6X7bU/7KlvmluonVuojUy9LLvwgue7tq/vr9pQ2fnN7pHqg8eqf3hN5/5ycbv/uTdTVt3L+gICghM4UibPzCZtuLpa65yNNc4uj1kgkB3TUsuYDS0tgww+dKAjaiSHHV3e5XsKFRVtsCcOHGiqamJuumXW3c3/voMTP9RGT7uP/X91rPf/NWnj27qePjR1x9+dPtC5zCLGJiubodD0rI9fc01fd02wwEj6al6u2ptXZID0dWJlScwx48fv/POO2+77TZWgqc2vQSTnZQA8HH/qd21bz72+G+3PWVn0qJ9MCABxmnlCEmAyW4immo2UgEdkVV6mzKx08pZ7E67Rcw2kySTgFU0CYxKHeSSAzMgvTwLF/L05dzT16zYxGxt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xjDMGlJj2mUbRuBMYkWCzAoVF6EwKBQOoTAoFA6hMCgUDqEwKBQOoTAoFA6hMCgUDpUTsAEB48Nty0JDh4rdUVKJ9mzNvjPTNFVTsAMty0Jnlk23LYkRzrZM4uc8u9G2VPG1IZnhOeqFFSUAhh3e1VLbVVLe2/6+4CtpdkRED57HY7aqpbaqpbaqq7sg0e9XfJIT19zOqaltqql1uZmFTZgk2alqIYQmh0BMUasTFFUPGB6enpCodBCcuivWzLctiR3J8MwphAP6OZsbbJnk7W3zPy2Y5XcigqMpAVngentkpAjYNDbJeFEiPT0NdMxIOTpa65qabZ1MVK626sc3R55LElvUVQ8YA4dOuRwOHw+n3oy7olj9237/d11ncu2d/39Cz1LfvXnG1s+4Z44BuKQ7OSy4b2qnQzLyZV9IjlXaxN9KLq1qIAh5elrrunzAngdDvrWnMDkSLkYgfF6vfv37x8dHVVJdnPVNmoQtvbXLQmevGXQPg9gyEcidfUwyt2ljsvswI14Ml9h0M/s67RyFrszvRvpmqQRSh2SkaYXDYsEMJ//F9JYrbTjIqUNmN4uSg8jKj/AUEZ05gYGAHw+n91uv3z5skrKu1rWsYIwKhu0q47K6HMYsiFI5zBqUxipuYto9QwTPN2zLDcHC9FCEVTTFyU3WYTGRQJcdmumCiorBMiOSyotwEgu/wM2xURFOocRp0MU5UYr0F2TrYDpgQGAsbGx+vr6QIB5mDfWrdp8bJsy3Fi3Kjh4rL9pyfixWwab2J0MrYeRdhlaxzNpsNJJxXwl9kwlitnkUim8zqzPtNzkEdo8z+RRMOlUHpdEOYFxt1MYCHTXEGxQMRDvDdT0ecVILX1Rb5e4i+mB8fv9OXuYG+tWsQIA9NctGT92S38dExiqJVfpMdY8AZBcmeVrFWVy45Q3r+gms+ICQ5qpmfuyjistGTCB7hoSDyotGYnDszwMyaTZGg6Y/wfOmUiG1HTB6wAAAABJRU5ErkJggg==" alt="" />
第一步:编写properties文件。(new 新建一个db.properties文件)
内容如下:
driver= com.mysql.jdbc.Driver
url= jdbc:mysql:///spring3_day2
username= root
password=root
第二步:编写Spring的applicationContext.xml配置文件
<!-- 引入peoperties文件
我们更多地是采用第一种写法,第二种写法不常用因为很复杂。 --> <!-- 引入peoperties文件的第一种写法 -->
<!-- <context:property-placeholder location="classpath:db.properties"/> -->
<!-- 引入peoperties文件的第二种写法 -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="classpath:db.properties"/>
</bean> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${driver}"/><!-- 这里的driver,url,username,password都是根据 db.peoperties文件写的-->
<property name="jdbcUrl" value="${url}"></property>
<property name="user" value="${username}"></property>
<property name="password" value="${password}"></property>
</bean> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
第三步:编写Juit测试代码。(jdbctest.java文件)
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext.xml") public class Jdbctest {
//为了注解(根据类型注解的),不加这个注释的话 Spring怎么给jdbcTemplate赋值
@Autowired
private JdbcTemplate jdbcTemplate;
@Test
public void jdbcTemplatetest()
{
jdbcTemplate.execute("create table person6(id int primary key,name varchar(20))"); } }